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
asked Jul 16, 2014 at 14:52
sujithvm
2,4413 gold badges17 silver badges16 bronze badges
1 Answer 1
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,))
answered Jul 16, 2014 at 14:58
CL.
182k18 gold badges241 silver badges282 bronze badges
default
footo the value inbar, do you dofoo = "bar"?