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, duedate 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?
asked Nov 4, 2010 at 15:21
Pwnna
9,60821 gold badges69 silver badges94 bronze badges
1 Answer 1
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)
answered Nov 4, 2010 at 15:30
mouad
70.5k18 gold badges117 silver badges106 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Pwnna
Thanks that solved it. I'm still in the php mysqli::stmt->prepare mindset
default