Python SQLite3 Problem with?
Im currently learning SQLite3 with Python. I'm looking at the manual, and it tells me to do something like the following:
data = (tablename, )
c.execute("CREATE TABLE IF NOT EXISTS ?(uid INTEGER PRIMARY KEY, title TEXT NOT NULL, due开发者_运维百科date INTEGER NOT NULL, description TEXT, archived INTEGER)", data)
I'm getting an error, however. It's stated as follows:
sqlite3.OperationalError: near "?": syntax error
What's going on?
sadly the DB-API’s parameter substitution ?
don't work with table name , columns name .. and it's the same in all DB API in python.
the DB-API’s parameter substitution just work for value like in SELECT * FROM table WHERE id = ?
, so you will have to do string formating or just put the name table in the string directly.
query = "CREATE TABLE IF NOT EXISTS %s (uid INTEGER PRIMARY KEY, title TEXT NOT NULL, duedate INTEGER NOT NULL, description TEXT, archived INTEGER)" % table_name
conn.execute(query)
精彩评论