简体   繁体   中英

Python write to MySQL - no error but no writing

I am trying to write into my localhost MySQL database.

I have created a database named "test", a table called "price_update" and a row called "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.

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',))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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