我有这个代码:
conn = sqlite3.connect("testDB")
curs = conn.cursor()
curs.execute("CREATE TABLE IF NOT EXISTS testTable( col1 VARCHAR, col2 VARCHAR)")
c1 = "value1"
c2 = "value2"
curs.execute("INSERT INTO testTable VALUES (?,?)", (c1, c2))#this works fine
conn.commit()
def inDB(curs, table, col, value):
curs.execute("SELECT * FROM ? WHERE ?=?",(table, col, value)) #problemis here
return bool(curs.fetchone())
print inDB(curs, "testTable", "col1", c1)
它给了我这个错误:
Traceback (most recent call last):
File "test.py", line 16, in <module>
print inDB(curs, "testTable", "col1", c1)
File "test.py", line 13, in inDB
curs.execute("SELECT * FROM ? WHERE ?=?",(table, col, value))
sqlite3.OperationalError: near "?": syntax error
为什么这不起作用,我该如何解决?