简体   繁体   English

Python写入MySQL - 没有错误,但没有写

[英]Python write to MySQL - no error but no writing

I am trying to write into my localhost MySQL database. 我正在尝试写入我的localhost MySQL数据库。

I have created a database named "test", a table called "price_update" and a row called "model" 我创建了一个名为“test”的数据库,一个名为“price_update”的表和一个名为“model”的行

When I run the script below I get no errors, however, I also get nothing written to my database. 当我运行下面的脚本时,我没有错误,但是,我也没有写入我的数据库。

I am not sure where to start looking for the problem. 我不知道从哪里开始寻找问题。 the row is varchar(10) and collation utf9_general_ci. 行是varchar(10)和collat​​ion utf9_general_ci。

import MySQLdb

conn = MySQLdb.connect(host="127.0.0.1",user="someUser",passwd="somePassword",db="test")

query = "INSERT INTO price_update (model) values ('12345')"
x = conn.cursor()
x.execute(query)
row = x.fetchall()

You have to commit the changes: 您必须提交更改:

conn.commit()

Also, I'd make your query safer: 另外,我会让您的查询更安全:

query = "INSERT INTO price_update (model) values (%s)"
...
x.execute(query, ('12345',))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM