MySQL 和 Python 数据库不会自动更新
我在更新 MySQL 数据库中的行时遇到一些问题。 这是我尝试运行的代码:
import MySQLdb
conn=MySQLdb.connect(host="localhost", user="root", passwd="pass", db="dbname")
cursor=conn.cursor()
cursor.execute("UPDATE compinfo SET Co_num=4 WHERE ID=100")
cursor.execute("SELECT Co_num FROM compinfo WHERE ID=100")
results = cursor.fetchall()
for row in results:
print row[0]
print "Number of rows updated: %d" % cursor.rowcount
cursor.close()
conn.close()
运行该程序时得到的输出是:
4
更新的行数:1
看起来它正在工作,但是如果我从 MySQL 命令行界面 (CLI) 查询数据库,我发现它根本没有更新。 但是,如果我从 CLI 输入 UPDATE compinfo SET Co_num=4 WHERE ID=100;
数据库将按预期更新。
我的问题是什么? 我在 Windows 机器上运行 Python 2.5.2 和 MySQL 5.1.30。
I'm having some trouble updating a row in a MySQL database. Here is the code I'm trying to run:
import MySQLdb
conn=MySQLdb.connect(host="localhost", user="root", passwd="pass", db="dbname")
cursor=conn.cursor()
cursor.execute("UPDATE compinfo SET Co_num=4 WHERE ID=100")
cursor.execute("SELECT Co_num FROM compinfo WHERE ID=100")
results = cursor.fetchall()
for row in results:
print row[0]
print "Number of rows updated: %d" % cursor.rowcount
cursor.close()
conn.close()
The output I get when I run this program is:
4
Number of rows updated: 1
It seems like it's working but if I query the database from the MySQL command line interface (CLI) I find that it was not updated at all. However, if from the CLI I enter UPDATE compinfo SET Co_num=4 WHERE ID=100;
the database is updated as expected.
What is my problem? I'm running Python 2.5.2 with MySQL 5.1.30 on a Windows box.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不确定,但我猜你正在使用 INNODB 表,并且你还没有完成提交。 我相信 MySQLdb 自动启用事务。
在调用
close
之前调用conn.commit()
。来自常见问题解答:从 1.2 开始.0,MySQLdb默认禁用自动提交
I am not certain, but I am going to guess you are using a INNODB table, and you haven't done a commit. I believe MySQLdb enable transactions automatically.
Call
conn.commit()
before callingclose
.From the FAQ: Starting with 1.2.0, MySQLdb disables autocommit by default
MySQLdb 默认情况下关闭自动提交,这一开始可能会令人困惑。 您的连接存在于其自己的事务中,在提交该事务之前,您将无法看到从其他连接所做的更改。
您可以像其他人指出的那样在更新语句之后执行
conn.commit()
,或者通过在创建后立即设置conn.autocommit(True)
来完全禁用此功能连接对象。MySQLdb has autocommit off by default, which may be confusing at first. Your connection exists in its own transaction and you will not be able to see the changes you make from other connections until you commit that transaction.
You can either do
conn.commit()
after the update statement as others have pointed out, or disable this functionality altogether by settingconn.autocommit(True)
right after you create the connection object.您需要手动提交更改或打开自动提交。
SELECT 返回修改(但未持久)数据的原因是因为连接仍处于同一事务中。
You need to commit changes manually or turn auto-commit on.
The reason SELECT returns the modified (but not persisted) data is because the connection is still in the same transaction.
我发现Python的连接器会自动关闭自动提交,并且似乎没有任何方法可以改变这种行为。 当然,您可以重新打开它,但是查看查询日志,它在连接后愚蠢地执行了两个毫无意义的查询,以关闭自动提交然后重新打开。
I've found that Python's connector automatically turns autocommit off, and there doesn't appear to be any way to change this behaviour. Of course you can turn it back on, but then looking at the query logs, it stupidly does two pointless queries after connect to turn autocommit off then back on.
连接器/Python 连接参数
打开自动提交可以在连接数据库时直接完成:
MySQLConnection.autocommit 属性
或者单独:
显式提交更改是通过
Connector/Python Connection Arguments
Turning on autocommit can be done directly when you connect to a database:
MySQLConnection.autocommit Property
Or separately:
Explicitly committing the changes is done with
我必须在我的 mysqlWorkbench 应用程序脚本上执行
SET autocommit=true
I have to execute
SET autocommit=true
on my mysqlWorkbench app script