Python 2.7 sqlite3.Cursor.execute 函数的问题

问题描述 投票:0回答: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:“?”附近:语法错误

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

python sql python-2.7 sqlite
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.