Python mysql更新,工作但不更新表

发布于 2025-01-29 15:40:01 字数 661 浏览 4 评论 0原文

我有一个需要更新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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖风昔人 2025-02-05 15:40:02

使用

dbb.commit()

curb.execute之后

(“更新radiogroups设置currentState = 1 whene radioID = 11”)

提交您“加载”到MySQL Server中的所有更改

use

dbb.commit()

after

curb.execute ("UPDATE RadioGroups SET CurrentState=1 WHERE RadioID=11")

to commit all the changes that you 'loaded' into the mysql server

月依秋水 2025-02-05 15:40:02

正如@lazykiddy指出的那样,将更改加载到MySQL之后,您必须提交更改。

在MySQL连接初始化之后,您也可以使用此方法启用自动提交设置:

dbb.autocommit(True)

然后,它将自动提交您在代码执行期间所做的更改。

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:

dbb.autocommit(True)

Then, it will automatically commit the changes you made during your code execution.

电影里的梦 2025-02-05 15:40:02

两个答案是正确的。但是,您也可以这样做:

dbb = MySQLdb.connect(host="localhost", 
   user="user", 
   passwd="pass", 
   db="database",
   autocommit=True) 

添加 autocommit = true

the two answers are correct. However, you can also do this:

dbb = MySQLdb.connect(host="localhost", 
   user="user", 
   passwd="pass", 
   db="database",
   autocommit=True) 

add autocommit=True

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文