2016-10-15 9 views
0

mysqlにデータを挿入するPythonスクリプトを作成しようとしていますが、テストしようとするとエラーが発生します。Pythonでmysql.connector経由で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 

誰も私が解決することができますこの問題?

+0

する必要があり、(curr_time)'それが「AAAのINSERT INTO 'でなければなりません"INSERT INTO aaa(data) VALUES (%s)", (curr_time)%,を置き換えます(データ)VALUES(%s) "%(curr_time)' –

答えて

0

それはあなたが ` "(データ)VALUES(%sの)AAA。INSERT INTO" で`% `を忘れ"INSERT INTO aaa(data) VALUES (%s)"%(curr_time)

関連する問題