【发布时间】:2012-02-09 02:53:17
【问题描述】:
使用 pysqlite 我正在制作一个程序来处理一些数据。对多表多列的相似字段做同样的操作,所以我想可以参数化sql语句如下图:
def foo():
column = 'c'
table = 't'
row = 1
# preferred approach, gives syntax error
c.execute('SELECT ? FROM ? WHERE id=?', (column, table, row))
# sanity check, works fine
c.execute('SELECT c FROM t WHERE id=?', (row))
# workaround, also works, but is this the right way?
c.execute('SELECT % FROM % WHERE id=?' % (column, table), row))
我得到的错误不是很有帮助 (sqlite3.OperationalError: near "?": syntax error),但我明白了:Pysqlite 不喜欢以这种方式使用占位符。
谁能指出这里发生了什么以及执行上述操作的正确方法?
【问题讨论】:
标签: python sql sqlite pysqlite