1

我有一个非常令人沮丧的问题

我有一个函数接受一个指向 sqlite3.Connection 对象的游标,然后修改数据库

我的代码是这样的:

#DOES NOT WORK
def update(dbc, tableName, value, value2):
    dbc.execute("update ? set MyValue = ? where Something = ?;",\
    [tableName, value, value2])

#WORKS
def update2(dbc, tableName value, value2):
    dbc.execute("update {0} set MyValue = {1} where Something = {2};".format(
    tableName, value, value2))

db = sqlite3.connect('data.db')
c = db.cursor()
c.execute("begin;")
update(c,"Something","Something Else") #FAILS
update2(c,"Something","Something Else") #OK

我得到错误:

sqlite3.OperationalError:靠近“?”:语法错误

我尝试注释掉第一个 execute("begin;") 语句,因为我不太了解它,但我知道它在其他部分显着加快了我的代码速度,而不必提交每一个输入。有谁知道为什么会这样?

4

1 回答 1

0

经过数小时的搜索,我在接下来的 10 分钟内找到了答案。不能以这种方式加载表名......看起来很傻。这是原始答案的链接: SQLite参数替换问题

于 2012-09-12T22:03:52.267 回答