Python不提交MySQL事务(Python not committing MySQL transaction)

编程入门 行业动态 更新时间:2024-10-11 17:22:08
Python不提交MySQL事务(Python not committing MySQL transaction)

我通过下面的python代码将几千条记录插入到表中:

values = '' for row in cursor: values = values + "(" + self.quoted_comma_separate(row) + ")," values = values[:-1] insert_statement = "INSERT INTO t1 ({0}) VALUES {1};".format( self.comma_separate(members), values) db = Database() conn = db.get_db() cursor = conn.cursor() cursor.execute(insert_statement) conn.commit() conn.close()

当我在运行数据库后检查数据库时,数据库中没有显示任何记录。 如果我进入MySQL编辑器并手动提交事务,则会显示所有记录。 为什么我的conn.commit()不起作用?

I am inserting a couple thousand records into a table via the python code below:

values = '' for row in cursor: values = values + "(" + self.quoted_comma_separate(row) + ")," values = values[:-1] insert_statement = "INSERT INTO t1 ({0}) VALUES {1};".format( self.comma_separate(members), values) db = Database() conn = db.get_db() cursor = conn.cursor() cursor.execute(insert_statement) conn.commit() conn.close()

When I check the database after it runs none of the records show up in the database. If I go into an MySQL editor and manually commit the transaction all of the records appear. Why is my conn.commit() not working?

最满意答案

插入语句很好。 事实证明我打开了另一个数据库连接,它变得混乱,并提交错误的连接或类似的东西。 对不起毫无疑问的问题:)

The insert statements were fine. Turns out I had another database connection open and it was getting confused and committing to the wrong connection or something like that. Sorry for the pointless question :)

更多推荐

本文发布于:2023-08-04 12:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415684.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事务   MySQL   Python   committing   transaction

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!