6

simply put i am trying to make a sql database table and input data into it. I have it working in a simpler way, but when I put it into my script it results in this error. I'm hoping its something simple 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 the 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

4
  • So what data do you have in xdates? Commented May 15, 2015 at 11:02
  • 3
    Also, why are you using string interpolation instead of SQL parameters? Commented May 15, 2015 at 11:02
  • 1
    Try printing the statement before executing it. I'm sure you'll immediately spot the error. Commented May 15, 2015 at 11:57
  • 3
    You're inviting Booby Tables to tea... bobby-tables.com Commented May 15, 2015 at 12:58

1 Answer 1

10

Simply put, let the DB API do that 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 is mentioned:

Instead, use the DB-API’s parameter substitution.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that has really helped. I guess im sticking too much to what I know instead of getting to know the packages im using!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.