{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "42e17205", "metadata": {}, "outputs": [], "source": [ "# %%\n", "import pymysql\n", "\n", "# %%\n", "\n", "# 创建数据库连接\n", "connection = pymysql.connect(\n", "host='192.168.0.244',\n", "port=3300,\n", "user='root',\n", "password='hxyd1109',\n", "database='school_server',\n", "charset='utf8'\n", ")\n", "cursor = connection.cursor()\n", "# %%\n", "def create_table(cursor,result):\n", " \n", " data = []\n", " for i in result:\n", " list_list = list(i)\n", " des=cursor.description # 获取表详情,字段名,长度,属性等\n", " t = \",\".join([item[0] for item in des])\n", " table_head = t.split(',') # # 查询表列名 用,分割\n", " \n", " dict_result = dict(zip(table_head, list_list)) # 打包为元组的列表 再转换为字典\n", " data.append(dict_result) # 将字典添加到list_result中\n", " return data\n", "\n", "# %%\n", "\n", "# %%\n", "\n", "# %%\n", "sql1 = \"select * from teacher where id=828\"\n", "cursor.execute(sql1)\n", "result=cursor.fetchall()\n", "table_app = create_table(cursor,result)" ] }, { "cell_type": "code", "execution_count": 3, "id": "54db4116", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'id': 828,\n", " 'tea_name': '彭兰英',\n", " 'sex': 2,\n", " 'phone': '15198039323',\n", " 'type': 6,\n", " 'grade_id': None,\n", " 'class_id': None,\n", " 'user_name': 'ptszxbr9023',\n", " 'team_id': '110',\n", " 'pid': None,\n", " 'serial_no': '1336827486',\n", " 'openid': None,\n", " 'tea_no': 'ptszxbr9023',\n", " 'wx_phone': '15198039323',\n", " 'user_id': None,\n", " 'department_id': None,\n", " 'zt_teacher_uuid': None,\n", " 'hire': '职工',\n", " 'power': '门卫安保',\n", " 'class_type_name': None,\n", " 'hire_id': 98,\n", " 'flag': None,\n", " 'state': 1,\n", " 'sync_source': 'local'}]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table_app" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }