代码
# -*- coding: utf-8 -*-
# @Author : zbz
import pymysql
connect = pymysql.connect(user="root", host="localhost", database="你的数据库", password="你的密码")
cursor = connect.cursor()
sql = "select * from table limit 10"
cursor.execute(sql)
infos = cursor.description
fileds = [info[0] for info in infos]
datas = cursor.fetchall()
for data in datas:
print(dict(zip(fileds, data)))

这段代码展示了如何使用Python的pymysql模块连接到本地MySQL数据库,执行SQL查询(例如:从表中选取前10条记录),并打印查询结果。它还展示了将查询结果转化为字典列表以便于处理。
4914

被折叠的 条评论
为什么被折叠?



