Python mysql更新,工作但不更新表
我有一个需要更新MySQL数据库的Python脚本,到目前为止,我已经
dbb = MySQLdb.connect(host="localhost",
user="user",
passwd="pass",
db="database")
try:
curb = dbb.cursor()
curb.execute ("UPDATE RadioGroups SET CurrentState=1 WHERE RadioID=11")
print "Row(s) were updated :" + str(curb.rowcount)
curb.close()
except MySQLdb.Error, e:
print "query failed<br/>"
print e
更新了脚本行:
,具有具有Radio> Radiod 代码>11。如果我将
RadioID
更改为表中不存在的另一个数字,则会说行已更新:0
。但是,数据库实际上并未更新。 Currentstate
字段仅保持不变。如果我将SQL语句复制到phpmyadmin,则可以正常工作。
I have a python script which needs to update a mysql database, I have so far:
dbb = MySQLdb.connect(host="localhost",
user="user",
passwd="pass",
db="database")
try:
curb = dbb.cursor()
curb.execute ("UPDATE RadioGroups SET CurrentState=1 WHERE RadioID=11")
print "Row(s) were updated :" + str(curb.rowcount)
curb.close()
except MySQLdb.Error, e:
print "query failed<br/>"
print e
The script prints Row(s) were updated :
with the correct number of rows which have a RadioID
of 11. If I change the RadioID
to another number not present in the table it will say Row(s) were updated :0
. However the database doesn't actually update. The CurrentState
field just stays the same. If I copy and past the SQL statement in to PHPMyAdmin it works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
在
curb.execute之后
(“更新radiogroups设置currentState = 1 whene radioID = 11”)
提交您“加载”到MySQL Server中的所有更改
use
after
curb.execute ("UPDATE RadioGroups SET CurrentState=1 WHERE RadioID=11")
to commit all the changes that you 'loaded' into the mysql server
正如@lazykiddy指出的那样,将更改加载到MySQL之后,您必须提交更改。
在MySQL连接初始化之后,您也可以使用此方法启用自动提交设置:
然后,它将自动提交您在代码执行期间所做的更改。
As the @Lazykiddy pointed out, you have to commit your changes after you load them into the mysql.
You could also use this approach to enable the auto commit setting, just after the MySQL connection initialization:
Then, it will automatically commit the changes you made during your code execution.
两个答案是正确的。但是,您也可以这样做:
添加 autocommit = true
the two answers are correct. However, you can also do this:
add autocommit=True