0

I have a dict like this:

{
    '05_23': '[L]',
    '05_22': '9:39',
    '05_21': '9:37',
    '05_20': '9:34',
    '05_27': '14:41'
}

I take the keys and values from it and create lists:

keys = list(d.keys())

print keys
['05_23', '05_22', '05_21', '05_20', '05_27']

values = list(d.values())

print values
['[L]', '9:39', '9:37', '9:34', '14:41']

And then I construct this SQL query with it:

c.execute("INSERT INTO table {} VALUES {}".format(tuple(keys), tuple(values)))

Which results in the following error:

c.execute("INSERT INTO table {} VALUES {}".format(tuple(keys), tuple(values)))

sqlite3.OperationalError: near "table": syntax error

And I can't figure out how to make it work.

4
  • 2
    table is a SQL keyword. You want the table name there. Commented Jun 24, 2015 at 11:31
  • 1
    What is your table name? Here is an example of SQL insert - INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...); you need to replace table_name with the name of your table Commented Jun 24, 2015 at 11:32
  • Can this be done? c.execute("INSERT INTO table_name (X) VALUES (:X)", d) Commented Jun 24, 2015 at 11:38
  • Thanks guys... that hand't occured to me... Commented Jun 24, 2015 at 11:44

1 Answer 1

1

table is not a valid table name. See the list of SQLite keywords.

The SQL standard specifies a huge number of keywords which may not be used as the names of tables, ...

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

1 Comment

Wow... that didn't occur to me. It works fine now. Thanks!

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.