【问题标题】:Python - Insert data to table not workingPython - 将数据插入表不起作用
【发布时间】:2020-04-27 00:33:05
【问题描述】:

我认为我的数据库有问题。 在我的 python 代码中,我能够从我的表中创建、删除和选择数据,但我无法插入任何数据。

我的执行功能

dbhandler.py

    def execute(self, sql):
        self.connection()
        self.cur.execute(sql)
        print(sql)
        self.disconnect()
dataanalysis.py

mysqldb.execute("""CREATE TABLE DATA(p1 int NOT NULL, p2 int NOT NULL);""")
-- Works fine --

mysqldb.execute("""INSERT INTO DATA(p1,p2) VALUES(1,2);""")
-- Nothing happens --

当我连接到我的管理员时,我可以看到表格很好,但没有插入任何数据。如果我在 adminer 中运行查询,数据会按预期插入到表中。

【问题讨论】:

  • 尝试提交您的事务。根据您使用的驱动程序,您需要运行类似mysqldb.commit() 或类似的东西。

标签: python mysql mysql-connector adminer


【解决方案1】:

为了反映表中的变化你需要运行commit()函数

ConnName.commit()

【讨论】:

    猜你喜欢
    • 2016-08-06
    • 2012-04-09
    • 2016-07-02
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 2017-10-07
    • 2014-08-07
    • 1970-01-01
    相关资源
    最近更新 更多