from pymysql import Connection
# 构建到Mysql数据库的连接
coon = Connection(
host='localhost',
port=3306,
user='......',
password='......',
# autocommit=True # 设置自动提交(确认)
)
# print(coon.get_server_info())
# 1. 执行非查询性质的SQL
# 1.1创建游标
cursor = coon.cursor()
# 1.2 通过游标选择数据库
coon.select_db("student_score")
# 2.执行查询性质的SQL
# 获取查询结果
result = cursor.fetchall()
for row in result:
print(row)
# 1. 执行非查询性质的SQL
# 创建游标
cursor = coon.cursor()
# 通过游标选择数据库
coon.select_db("student_score")
# 插入数据, 需要 commit 确认
cursor.execute("insert into detail(name,no, homeTown, chinese, math, english, total) values('xx','211212','beijing',80,90,85,255)")
# 通过 commit 确认
coon.commit()
coon.close() # 关闭连接
注意:这边一定要commit, 不然没用
也可以设置自动提交,就不用手动提交了
?