Python insert variable to mysql via mysql.connector
<p>I am trying to create one python script to insert data into mysql, but I got an error when I try to test it.</p>

<p>This is how I create the table:</p>

<pre><code>CREATE TABLE aaa (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     data CHAR(30) NOT NULL,
     PRIMARY KEY (id)
);
</code></pre>

<p>This is my python script:</p>

<pre><code>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()
</code></pre>

<p>The error is like this:</p>

<pre><code>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
</code></pre>

<p>Can anyone help me solve this problem?</p>

<p>replace <code>,</code> with <code>%</code> in  <code>"INSERT INTO aaa(data) VALUES (%s)", (curr_time)</code> it should be <code>"INSERT INTO aaa(data) VALUES (%s)"%(curr_time)</code></p>

python mysql