I use mysql.connector
Python 中的库将查询发送到数据库。但是,当数据库在初始化后发生更改时,mysql.connector
工具的回答就像数据库从未改变过一样。
例如,假设我有一张极简主义的桌子students
只有两列id
and name
.
+----+------+
| id | name |
+----+------+
| 0 | foo |
+----+------+
在下面的代码中,查询将询问用户id
0. 但是,在进程内部,Python 脚本外部会发生一些事件并更改数据库。
import mysql.connector
maindb = mysql.connector.connect(
host = "<host>",
user = "<user>",
password = "<password>",
db = "<database name>"
)
cursor = maindb.cursor()
# Here, I will send outside the python script a MySQL query to modify the name of the student from “foo” to “bar” like this:
# `UPDATE `students` SET `name` = 'bar' WHERE `students`.`id` = 0;`
cursor.execute("SELECT `id`, `name` FROM `students` WHERE `id` = 0")
result = cursor.fetchall()
print(result)
然后我得到这个答案[(0, 'foo')]
。正如你所看到的,Python 并不知道数据库从那时起就发生了变化。maindb.cursor()
被称为。所以我得到foo
作为名称字段而不是bar
正如预期的那样。
那么如何告诉mysql.connector
当我发送查询时,有什么工具可以从数据库中获取最新更新?
您将需要使用套接字,或者如果更改频繁发生,则您的代码每 x 分钟重新运行一次
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)