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 :)
更多推荐
发布评论