Python通过mysql.connector将变量插入mysql

时间:2016-10-15 02:47:55

标签: 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 = 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

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

Traversal替换为,中的% "INSERT INTO aaa(data) VALUES (%s)", (curr_time)