I am trying to create one python script to insert data into mysql, but I got an error when I try to test it.
This is how I create the table:
CREATE TABLE aaa (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
data CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
This is my python script:
import mysql.connector
from time import strftime, gmtime, sleep
cnx = mysql.connector.connect(
user='root', password='abcd', database='test_db'
)
cur = cnx.cursor()
# get current timestamp
curr_time = strftime("%Y%m%d_%H%M%S", gmtime())
cur.execute(
"INSERT INTO aaa(data) VALUES (%s)", (curr_time)
)
cnx.commit()
cnx.close()
The error is like this:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s)' at line 1
Can anyone help me solve this problem?
replace ,
with %
in "INSERT INTO aaa(data) VALUES (%s)", (curr_time)
it should be "INSERT INTO aaa(data) VALUES (%s)"%(curr_time)
. @tsh is correct use this (curr_time) -> (curr_time,)
instead
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.