2

I am trying to make a sqlite database in python using sqlite3. Here i am creating new tables with names specified in table_names each having only one field column. conn.execute('CREATE TABLE {} (quote TEXT PRIMARY KEY NOT NULL);'.format(table_names[i])) and quote i am reading from a file. However i am not able to execute insert statement and below i have posted what all i have tried and their errors. Any help will be appreciated. Thanks

conn.execute('''INSERT INTO {} (quote) VALUES ('{}')'''.format(table_names[i], quote))
Error : sqlite3.OperationalError: near "s": syntax error

conn.execute('''INSERT INTO {} (quote) VALUES ('?')'''.format(table_names[i]), (quote,))
Error : sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 1 supplied.

conn.execute('''INSERT INTO ? (quote) VALUES ('?')''', (table_names[i], quote))
Error : sqlite3.OperationalError: near "?": syntax error
1
  • If you are trying to assign variable foo to the value in bar, do you do foo = "bar"? Commented Jul 16, 2014 at 15:50

1 Answer 1

2

The parameter marker ? must not be quoted, because in SQL, everything in quotes is a string.

The correct form is:

conn.execute('''INSERT INTO {} (quote) VALUES (?)'''.format(table_names[i]), (quote,))
Sign up to request clarification or add additional context in comments.

2 Comments

But the value they are trying to add is a string.
The parameter's value is a string, the parameter itself is an abstract variable.

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.