Python MySQLdb 更新查询失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1028671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python MySQLdb update query fails
提问by sqram
Okay. I've built here a mysql query browser, like navicat. Using MySQLdb to perform queries.
好的。我在这里构建了一个 mysql 查询浏览器,比如 navicat。使用 MySQLdb 执行查询。
Here's the weird part. When i run the query through the program(using MySQLdb), it gives me success, affected rows = 1, but when i look at it in phpmyadmin, the value hasn't changed.
这是奇怪的部分。当我通过程序(使用 MySQLdb)运行查询时,它给了我成功,受影响的行数 = 1,但是当我在 phpmyadmin 中查看它时,该值没有改变。
so before i perform the query, i print it out, copy and paste into phpmyadmin's query window, hit go and it works. So long story short, update query isn't working, but when i copy and paste into phpmyadmin, it works.
所以在我执行查询之前,我将它打印出来,复制并粘贴到 phpmyadmin 的查询窗口中,点击开始,它就可以工作了。长话短说,更新查询不起作用,但是当我复制并粘贴到 phpmyadmin 时,它起作用了。
self.tbl.sql.use(self.tbl.database) # switches to correct database. I've printed this and it uses the corrected db
if self.tbl.sql.execute(query) == True:
print sql_obj.rows_affected() # returns 1 (since i only do 1 query)
And here's the part of the SQL class
这是 SQL 类的一部分
def execute(self, query):
try:
self.cursor.execute(query)
return True
except MySQLdb.ProgrammingError as error:
print "---->SQL Error: %s" % error
return False
except MySQLdb.IntegrityError as e:
print "--->SQL Error: %s" % e
return False
So any ideas what could be happening?
那么有什么想法可能会发生吗?
回答by codeape
I believe @Jason Creighton and @S.Lott are correct.
我相信@Jason Creighton 和@S.Lott 是正确的。
At least if the table that you're updating is on a transactional storage engine. InnoDBis transactional, ISAMis not.
至少如果您要更新的表位于事务存储引擎上。InnoDB是交易性的,ISAM不是。
You either have to call commit()on your connection object before closing it, or you must set the connection to autocommit mode. I am not sure how you do that for a MySQLdb connection, I guess you either set an argument to the connection constructor, or set a property after creating the connection object.
您要么必须commit()在关闭连接对象之前调用它,要么必须将连接设置为自动提交模式。我不确定您如何为 MySQLdb 连接执行此操作,我猜您要么为连接构造函数设置一个参数,要么在创建连接对象后设置一个属性。
Something like:
就像是:
conn = mysql.connection(host, port, autocommit=True)
# or
conn = mysql.connection(host, port)
conn.autocommit(True)
回答by Jason Creighton
Just a guess: Perhaps the code in Python is running within a transaction, and the transaction might need to be explicitly committed?
只是猜测:也许 Python 中的代码在事务中运行,并且该事务可能需要显式提交?
Edit: There's an entry in the MySQLdb FAQthat might be relevant.
编辑:MySQLdb 常见问题解答中有一个可能相关的条目。

