gpt4 book ai didi

python - 无法插入mysql数据库

转载 作者:太空宇宙 更新时间:2023-11-03 11:56:26 25 4
gpt4 key购买 nike

我刚开始在 python 中使用 MySQLdb。我能够创建表,但是当我尝试插入时,没有行插入到表中,它显示该表仍然是空的。

    import MySQLdb
db = MySQLdb.connect("localhost","root","shivam","test")
cursor = db.cursor()
s = "DROP TABLE IF EXISTS batting"
cursor.execute(s)
s = """create table batting (
name varchar(50) primary key,
matches integer(5),
innings integer(5),
runs integer(5),
highest integer(3),
strikerate integer(3),
hundreds integer(3),
fifties integer(3)
)"""
cursor.execute(s)
s = """insert into batting(name) values(
"shivam")"""

cursor.execute(s)
db.close()

我可能哪里出错了?

最佳答案

您忘记提交您的连接。只需添加:

cursor.execute(s)
db.commit()

看看this .它解释了为什么您需要提交

关于python - 无法插入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32374077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com