1

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?

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that solved it. I'm still in the php mysqli::stmt->prepare mindset

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.