Sqlite3. Substitution of names in query.

Lacrima lacrima.maxim at gmail.com
Fri Oct 30 08:10:55 EDT 2009


Hello!

I use sqlite3 module for my sqlite database. I am trying to substitute
table name in sql query.

>>> import sqlite3
>>> con = sqlite3.connect('mydb')
>>> cur = con.execute("select * from table where name='Joe'")
That's ok

>>> cur = con.execute("select * from table where name=?", ('Joe',))
That's ok too

>>> cur = con.execute("select * from ? where name=?", ('table', 'Joe'))
Traceback (most recent call last):
  File "<string>", line 1, in <fragment>
sqlite3.OperationalError: near "?": syntax error

So what's wrong now?
Is it impossible to substitute table names, using DB API?
If so, what is a way to make table name substitutions? Are string
operations like
'select * from %s where...' % tablename
ok in this case?

Thanks in advance!



More information about the Python-list mailing list