python如何在sql中使用变量

时间:2019-01-27 22:59:39

标签: python sql sqlite

我有一个数据库:

datas.db --->表bk

  • Id-自动编号
  • 名称-字符串
  • 数字-数字
  • 计时器-字符串

编码Python

self.db = sqlite3.connect("library.db")
self.cur = self.db.cursor()

search_name = self.lineEdit_1.text()
name = self.lineEdit_2.text()
number_age = self.lineEdit_3.text()
description = self.lineEdit_4.text()

self.cur.execute("UPDATE bk SET (Name ='?',Number =?, Timer ='?') WHERE Name ='?'",(name,number_age,description,search_name))
self.db.commit()

现在我收到语法错误,我已经看到他们使用了这个

"Name ='%s'"%name)

但是当我有多个变量时,我不知道如何使用它

1 个答案:

答案 0 :(得分:0)

根据SQLLite Docs,您的代码应如下所示。

您的查询错误,请更改为

UPDATE bk SET (Name ='?',Number =?, Timer ='?') WHERE Name ='?'

UPDATE bk SET Name=?, Number=?, Timer=? WHERE Name=?
self.db = sqlite3.connect("library.db")
self.cur = self.db.cursor()

search_name = self.lineEdit_1.text()
name = self.lineEdit_2.text()
number_age = self.lineEdit_3.text()
description = self.lineEdit_4.text()

self.cur.execute("UPDATE bk SET Name=?, Number=?, Timer=? WHERE Name =?",(name, number_age, description, search_name))
self.db.commit()