"""
Auther:少校
Time:2025/4/18 10:58
越努力,越幸运
"""
import pymysql
# 1. 创建数据库连接
sql1 = pymysql.connect(
user='root', #用户名
password='shaoxiao', #密码
host='127.0.0.1', #IP地址
port=3306, #端口号
database='school', #默认连接数据库/多个数据库默认哪个
charset='utf8mb4', #编码
autocommit=False #是否自动提交,默认关
)
# 2. 获取游标对象
s1 = sql1.cursor()
# 3. 执行有结果的sql语句
s1.execute('select * from students;')
# 4. 获取sql执行结果
# sql执行的结果,默认保存在游标对象中。
# 1)从游标对象中获取记录, 像迭代器一样取一条少一条。
# result = s1.fetchone()
# print(result)
# result = s1.fetchmany(3)
# print(result)
# result = s1.fetchall()
# print(result)
# 2)也可以直接转类型再去操作
# print(list(s1))14.py获取数据库查询结果
本节755字2025-04-18 17:09:41