MySQL 和 Python 数据库不会自动更新

发布于 2024-07-10 19:54:47 字数 744 浏览 8 评论 0原文

我在更新 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 技术交流群。

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

发布评论

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

评论(6

圈圈圆圆圈圈 2024-07-17 19:54:47

我不确定,但我猜你正在使用 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 calling close.

From the FAQ: Starting with 1.2.0, MySQLdb disables autocommit by default

夏见 2024-07-17 19:54:47

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 setting conn.autocommit(True) right after you create the connection object.

蹲墙角沉默 2024-07-17 19:54:47

您需要手动提交更改或打开自动提交。

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.

千笙结 2024-07-17 19:54:47

我发现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.

不交电费瞎发啥光 2024-07-17 19:54:47

连接器/Python 连接参数

打开自动提交可以在连接数据库时直接完成:

import mysql.connector as db
conn = db.connect(host="localhost", user="root", passwd="pass", db="dbname", autocommit=True)

MySQLConnection.autocommit 属性

或者单独:

import MySQLdb

conn = MySQLdb.connect(host="localhost", user="root", passwd="pass", db="dbname")
cursor = conn.cursor()
conn.get_autocommit()        # will return **False**
conn.autocommit(True)        # will make it True
conn.get_autocommit()        # Should return **True** now
cursor = conn.cursor()

显式提交更改是通过

conn.commit()

Connector/Python Connection Arguments

Turning on autocommit can be done directly when you connect to a database:

import mysql.connector as db
conn = db.connect(host="localhost", user="root", passwd="pass", db="dbname", autocommit=True)

MySQLConnection.autocommit Property

Or separately:

import MySQLdb

conn = MySQLdb.connect(host="localhost", user="root", passwd="pass", db="dbname")
cursor = conn.cursor()
conn.get_autocommit()        # will return **False**
conn.autocommit(True)        # will make it True
conn.get_autocommit()        # Should return **True** now
cursor = conn.cursor()

Explicitly committing the changes is done with

conn.commit()
呆° 2024-07-17 19:54:47

我必须在我的 mysqlWorkbench 应用程序脚本上执行 SET autocommit=true

I have to execute SET autocommit=true on my mysqlWorkbench app script

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