Sqlite3 "OperationalError: near" (": syntax error" python

just put, I am trying to make a sql database table and inject data into it. It works for me more simply, but when I put it in my script it results in this error. I hope this is something simple that I missed. Any help / advice would be greatly appreciated.

conn = sqlite3.connect('Data1.db')

c = conn.cursor()

# Create table
c.execute('''CREATE TABLE Data_Output6
         (date text, output6MV real)''') 

Averages_norm = []

for i, x in enumerate(Averages):
    Averages_norm.append(x*output_factor)
    c.execute("INSERT INTO Data_Output6 VALUES (%r,%r)" %(xdates[i],Averages_norm[-1]))
conn.commit()

      

results in an error:

57     for i, x in enumerate(Averages):
58         Averages_norm.append(x*output_factor)
---> 59         c.execute("INSERT INTO Data_Output6 VALUES (%r,%r)"%(xdates[i],Averages_norm[-1]))
60     conn.commit()
61 

      

OperationalError: near "(": syntax error

+3


source to share


1 answer


Simply put, let the DB API do this formatting:

c.execute("INSERT INTO Data_Output6 VALUES (?, ?)", (xdates[i], Averages_norm[-1]))

      



And refer to the documentation https://docs.python.org/2/library/sqlite3.html where it says:

Use DB-API Parameter Substitution instead.

+4


source







All Articles