I have the following code
cur.executemany("INSERT INTO "+tablename+" (name,"+item+") VALUES(?,?)",(data,))
Which dynamically inserts values from data
Now i have two problems :
If i use the syntax as VALUES(?,?)",(data))
without , after data
i would get this error
Error Incorrect number of bindings supplied. The current statement uses 2, and there are 4 supplied.:
which is solved only by using the syntax as VALUES(?,?)",(data,))
with ,
It solves this issue and data gets inserted into the table. But it creates another problem which i can't Query the DB and use something like
cursor = cur.execute("select * from "+tablename+" where name="+name+"")
i would get this error :
Error no such column: deek:
I Don't know how to Query the DB using the above mentioned Syntax.
I got the above syntax from sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied
Complete Code for reference :
import sqlite3
import sys
tablename='asfoor'
table_data=['Name','a','b','c']
try:
con = sqlite3.connect('dbtrials.db')
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS "+tablename+"")
cur.execute("CREATE TABLE "+tablename+" (ID INTEGER PRIMARY KEY AUTOINCREMENT ,Name TEXT, "+table_data[1]+" TEXT, "+table_data[2]+" TEXT, "+table_data[3]+" TEXT )")
name='deek'
item='a'
data=[name,item]
cur.executemany("INSERT INTO "+tablename+" (name,"+item+") VALUES(?,?)",(data,))
cursor = cur.execute("select * from "+tablename+" where name="+name+"")
for row in cursor :
print(row)
except sqlite3.Error as e:
print ("Error %s:" % e.args[0])
sys.exit(1)
finally:
if con:
con.close()