Python PyMysql 更新查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17758074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PyMysql UPDATE query
提问by Shay
I've been trying using PyMysql and so far everything i did worked (Select/insert) but when i try to update it just doesn't work, no errors no nothing, just doesn't do anything.
我一直在尝试使用 PyMysql,到目前为止我所做的一切都有效(选择/插入),但是当我尝试更新它时,它不起作用,没有错误,什么也没有,只是什么都不做。
import pymysql
connection = pymysql.connect(...)
cursor = connection.cursor()
cursor.execute("UPDATE Users SET IsConnected='1' WHERE Username='test'")
cursor.close()
connection.close()
and yes I've double checked that Users, IsConnected and Username are all correct and test does exist (SELECT works on it)
是的,我已经仔细检查过用户、IsConnected 和用户名是否都正确并且测试确实存在(SELECT 对其进行了处理)
what's my problem here?
我的问题是什么?
采纳答案by Joe Day
When you execute your update, MySQL is implicitly starting a transaction. You need to commit this transaction by calling connection.commit()
after you execute your update to keep the transaction from automatically rolling back when you disconnect.
当您执行更新时,MySQL 会隐式启动一个事务。您需要connection.commit()
在执行更新后通过调用来提交此事务,以防止事务在断开连接时自动回滚。
MySQL (at least when using the InnoDB engine for tables) supports transactions, which allow you to run a series of update/insert statements then have them either all commit at once effectively as a single operation, or rollback so that none are applied. If you do not explicitly commit a transaction, it will rollback automatically when you close your connection to the database.
MySQL(至少在对表使用 InnoDB 引擎时)支持事务,它允许您运行一系列更新/插入语句,然后将它们作为单个操作一次有效地提交,或者回滚以便不应用任何语句。如果您没有明确提交一个事务,当您关闭与数据库的连接时,它会自动回滚。
回答by saaj
In fact, what @JoeDay has described above has little to do with default MySQL transaction's behaviour. MySQL by default operates in auto-commit modeand normally you don't need any additional twist to persist your changes:
事实上,@JoeDay 上面所描述的与默认 MySQL 事务的行为几乎没有关系。MySQL 默认在自动提交模式下运行,通常你不需要任何额外的改动来持久化你的更改:
By default, MySQL runs with autocommit mode enabled. This means that as soon as you execute a statement that updates (modifies) a table, MySQL stores the update on disk to make it permanent. The change cannot be rolled back.
默认情况下,MySQL 在启用自动提交模式的情况下运行。这意味着一旦您执行更新(修改)表的语句,MySQL 就会将更新存储在磁盘上以使其永久化。更改无法回滚。
PEP-249's (DB API) authors decided to complicate things and break Zen of Python by making a transaction's start implicit, by proposing auto-commit to be disabled by default.
PEP-249(DB API)的作者决定通过提议默认禁用自动提交来使事务的启动隐式,从而使事情复杂化并打破 Python 的 Zen。
What I suggest to do, is to restore MySQL's default behaviour. And use transactions explicitly, only when you need them.
我建议做的是恢复 MySQL 的默认行为。并且仅在需要时才显式使用事务。
import pymysql
connection = pymysql.connect(autocommit=True)
I've also written about it herewith a few references.
我也在这里写过一些参考资料。