首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法插入到mysql数据库中

问无法插入到mysql数据库中
EN

Stack Overflow用户
提问于 2015-09-03 19:14:11
回答 1查看 50关注 0票数 0

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

代码语言:javascript
复制
    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()

我可能会出错的地方吗?

EN

回答 1

Stack Overflow用户

发布于 2015-09-03 19:15:40

你忘记commit你的连接了。只需添加:

代码语言:javascript
复制
cursor.execute(s)
db.commit()

看看this吧。它解释了为什么您需要使用commit

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32374077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档