Python通过mysql.connector将变量插入mysql
我试图创建一个python脚本来将数据插入mysql,但在测试它时出错 以下是我创建表的方式:Python通过mysql.connector将变量插入mysql,python,mysql,Python,Mysql,我试图创建一个python脚本来将数据插入mysql,但在测试它时出错 以下是我创建表的方式: CREATE TABLE aaa ( id MEDIUMINT NOT NULL AUTO_INCREMENT, data CHAR(30) NOT NULL, PRIMARY KEY (id) ); 这是我的python脚本: import mysql.connector from time import strftime, gmtime, sleep cnx =
CREATE TABLE aaa (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
data CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
这是我的python脚本:
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()
错误如下所示:
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
有人能帮我解决这个问题吗?在
“插入aaa(数据)值(%s)”,(当前时间)
中用%替换,
,应该是“插入aaa(数据)值(%s)”%(当前时间)
。
@tsh正确使用此(curr\u time)->(curr\u time)
而不是您在中忘记了%
“插入aaa(数据)值(%s)”,(curr\u时间)
它应该是“插入aaa(数据)值(%s)”%(curr\u时间)
(curr\u time)
(当前时间)