I have a python script which needs to update a mysql database, I have so far:
我有一个python脚本,需要更新一个mysql数据库,我到目前为止:
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.
脚本打印行已更新:具有RadioID为11的正确行数。如果我将RadioID更改为表中不存在的另一个数字,则表示行已更新:0。但是,数据库实际上并未更新。 CurrentState字段保持不变。如果我将SQL语句复制并传递给PHPMyAdmin,它可以正常工作。
3 个解决方案
#1
75
use
使用
dbb.commit()
after
后
curb.execute ("UPDATE RadioGroups SET CurrentState=1 WHERE RadioID=11")
curb.execute(“UPDATE RadioGroups SET CurrentState = 1 WHERE RadioID = 11”)
to commit all the changes that you 'loaded' into the mysql server
将您“加载”的所有更改提交到mysql服务器
#2
22
As the @Lazykiddy pointed out, you have to commit your changes after you load them into the mysql.
正如@Lazykiddy指出的那样,你必须在将它们加载到mysql之后提交你的更改。
You could also use this approach to enable the auto commit setting, just after the MySQL connection initialization:
在MySQL连接初始化之后,您也可以使用此方法启用自动提交设置:
dbb.autocommit(True)
Then, it will automatically commit the changes you made during your code execution.
然后,它将自动提交您在代码执行期间所做的更改。
#3
2
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
添加autocommit = True
#1
75
use
使用
dbb.commit()
after
后
curb.execute ("UPDATE RadioGroups SET CurrentState=1 WHERE RadioID=11")
curb.execute(“UPDATE RadioGroups SET CurrentState = 1 WHERE RadioID = 11”)
to commit all the changes that you 'loaded' into the mysql server
将您“加载”的所有更改提交到mysql服务器
#2
22
As the @Lazykiddy pointed out, you have to commit your changes after you load them into the mysql.
正如@Lazykiddy指出的那样,你必须在将它们加载到mysql之后提交你的更改。
You could also use this approach to enable the auto commit setting, just after the MySQL connection initialization:
在MySQL连接初始化之后,您也可以使用此方法启用自动提交设置:
dbb.autocommit(True)
Then, it will automatically commit the changes you made during your code execution.
然后,它将自动提交您在代码执行期间所做的更改。
#3
2
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
添加autocommit = True