为什么这个python类方法不起作用?(使用SQLite3)

2024-04-23 09:59:16 发布

您现在位置:Python中文网/ 问答频道 /正文

def update_data(self):

    db = sqlite3.connect("SQLite database")
    cursor = db.cursor()
    cursor.execute("""UPDATE Item SET ? = ? WHERE itemid = ? """,(self.field, self.value, self.ID))
    db.commit()
    cursor.close()

该错误表示“?”附近有语法错误但我不明白问题是什么。你知道吗

注意:这是一个具有完全定义属性的类的方法。你知道吗

非常感谢


Tags: selfexecutedbsqlitedatadefconnectupdate
1条回答
网友
1楼 · 发布于 2024-04-23 09:59:16

?字符不会在它出现的任何地方被替换,只在可以放置表达式的地方被替换。因此,尝试使用?作为列名是sqlite3语法错误。你知道吗

你可以用

query = 'update Item set {} = ? where itemid = ?'.format(self.field)
cursor.execute(query, (self.value, self.ID))

当然,这里需要擦洗self.field以防注射攻击。你知道吗

相关问题 更多 >