【发布时间】:2018-12-23 21:38:42
【问题描述】:
在我的 Windows 机器上,我在 MariaDB (10.3.7) 上有一个非常简单的数据库,我使用 mysql-connector-python-rf (2.2.2) 连接到该数据库。
我还使用 2 个 HeidiSQL 工作台实例连接到数据库。
当我使用其中一个工作台在数据表中添加或删除一行时,我可以立即在另一个工作台中使用 SELECT 语句访问更改的数据。我的结论是:第一个工作台已经将更改提交到数据库。
但是,看到 Python 的变化似乎更加复杂。我必须在查询前添加commit() 才能看到更改:
config = {'user' : 'some_user',
'password': 'some_password',
'host' : '127.0.0.1',
'database': 'some_database',
'raise_on_warnings': True,
}
db = mysql.connector.connect(**config)
# wait some to make changes to the database using the HeidiSQL workbenches
db.commit() # even though Python has not changed anything which needs to be
# committed, this seems necessary to re-read the db to catch
# the changes that were committed by the other clients
cursor = db.cursor()
cursor.execute('some_SQL_query')
for result in cursor:
do_something_with(result)
cursor.close()
到目前为止,我认为commit() 用于提交 Python 想要对数据库进行的更改。
说commit() 也将自上次connect() 以来其他客户端所做的更改读入Python 是否正确?这是错误/不便还是功能?
或者我错过了什么?
【问题讨论】:
-
您要查找的术语是“事务隔离级别”(起点:en.wikipedia.org/wiki/Isolation_(database_systems))
-
@brunodesthuilliers 很有趣……但这不是与并发事务有关吗?就我而言,交易是顺序的(主要是由于我使用鼠标和键盘的处理速度有限)。工作台 1 更改并提交,暂停,工作台 2 看到更改,暂停,Python 直到我在 Python 中 commit() 才看到更改。这是否意味着 Python 正在缓存数据,直到 commit() 强制它刷新缓存?
-
您搜索的不够多。 dev.mysql.com/doc/refman/8.0/en/…
标签: python mysql mariadb commit