gpt4 book ai didi

python - MySQL 表为空,尽管已通过 Python 填充

转载 作者:行者123 更新时间:2023-11-28 21:32:22 27 4
gpt4 key购买 nike

我编写了一个 python 脚本来通过 Excel 数据填充 MySQL 表。在 Python 脚本的末尾,我打印出表格的内容:

mycursor.execute("SELECT * FROM Cities")

rows=mycursor.fetchall()

for r in rows:
print r

我得到了正确的返回结果:

(1, u'Muenchen', 2.0, 3.32, 1, 1)
(2, u'Augsburg', 0.7, 1.008, 1, 1)
(3, u'Frankfurt', 1.1, 1.694, 1, 2)
(4, u'Wiesbaden', 0.3, 0.405, 1, 2)

但是当我通过 MySQL Workbench 打开该表时,它是空的。通过常规终端我得到相同的结果:

mysql> SELECT * FROM Cities;
Empty set (0.00 sec)

有什么问题吗?

最佳答案

您需要COMMIT更改

MySQLConnection.commit() Method

This method sends a COMMIT statement to the MySQL server, committing the current transaction. Since by default Connector/Python does not autocommit, it is important to call this method after every transaction that modifies data for tables that use transactional storage engines.

>>> cursor.execute("INSERT INTO employees (first_name) VALUES (%s)", ('Jane'))
>>> cnx.commit()
>>> cnx.close()

关于python - MySQL 表为空,尽管已通过 Python 填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56707299/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com