gpt4 book ai didi

python - 在 Python 中使用 MySQL 命令

转载 作者:行者123 更新时间:2023-11-29 10:32:01 24 4
gpt4 key购买 nike

我刚刚进入 SQL 做一些数据科学,并且想知道为什么我的代码正在运行但没有以任何方式影响 MySQL 数据库。我正在使用 pycharm 和 MySQLdb 模块。

 import MySQLdb
db = MySQLdb.connect(host="localhost",
user="root",
passwd="********", #Password blocked
db="test")
cur = db.cursor()
cur.execute("SELECT * FROM movies")
cur.execute("Update movies set genre = 'action' where id = 1")
for row in cur.fetchall() :
print row[0], " ", row[1], " ", row[2]

我的代码运行并没有返回错误,但是当我删除

 cur.execute("Update movies set genre = 'action' where id = 1")

line 它只是像以前一样打印出表格。仅供引用,下表如下:

1 星际科幻

2 雷神:诸神黄昏行动

3 雷神:黑暗世界 Action

如何让 python 中的命令真正影响表?非常感谢您的帮助!

最佳答案

您必须使用 cur.commit() 提交对表所做的更改

Database does not update automatically with MySQL and Python

关于python - 在 Python 中使用 MySQL 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47233366/

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