2.8 KiB
2.8 KiB
In [2]:
# %%
import pymysql
# %%
# 创建数据库连接
connection = pymysql.connect(
host='192.168.0.244',
port=3300,
user='root',
password='hxyd1109',
database='school_server',
charset='utf8'
)
cursor = connection.cursor()
# %%
def create_table(cursor,result):
data = []
for i in result:
list_list = list(i)
des=cursor.description # 获取表详情,字段名,长度,属性等
t = ",".join([item[0] for item in des])
table_head = t.split(',') # # 查询表列名 用,分割
dict_result = dict(zip(table_head, list_list)) # 打包为元组的列表 再转换为字典
data.append(dict_result) # 将字典添加到list_result中
return data
# %%
# %%
# %%
sql1 = "select * from teacher where id=828"
cursor.execute(sql1)
result=cursor.fetchall()
table_app = create_table(cursor,result)In [3]:
table_appOut [3]:
[{'id': 828,
'tea_name': '彭兰英',
'sex': 2,
'phone': '15198039323',
'type': 6,
'grade_id': None,
'class_id': None,
'user_name': 'ptszxbr9023',
'team_id': '110',
'pid': None,
'serial_no': '1336827486',
'openid': None,
'tea_no': 'ptszxbr9023',
'wx_phone': '15198039323',
'user_id': None,
'department_id': None,
'zt_teacher_uuid': None,
'hire': '职工',
'power': '门卫安保',
'class_type_name': None,
'hire_id': 98,
'flag': None,
'state': 1,
'sync_source': 'local'}]