
fetchall()返回的是元组类型

插入数据需要进行确认(commit)或者设置自动确认:


from pymysql import Connection
# 构建mysql数据库的连接(构建对象)
conn = Connection(
host='localhost',
port=3306,
user='root',
password='123456'
)
# print(conn.get_server_info())
# 执行非查询性质的MySQL语句
cursor = conn.cursor() # 获取游标对象
conn.select_db("world") # 选择数据库
# cursor.execute("create table test_pymysql(id int);")
# 执行查询性质的MySQL语句
cursor.execute("select * from student;")
results: tuple = cursor.fetchall()
for r in results:
print(r)
# 关闭数据库
conn.close()
实战案例:

本文展示了如何使用Python的pymysql模块建立数据库连接,执行查询和非查询SQL语句,特别是使用fetchall获取所有查询结果,并强调了数据提交(commit)的重要性。
1275

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



