python Sqlite3参数subs
我有一个小问题,这个类处理我的数据库。它仍然说:python Sqlite3参数subs,python,sqlite,Python,Sqlite,我有一个小问题,这个类处理我的数据库。它仍然说: cursor.execute(sql) ValueError: operation parameter must be str 我尝试了很多东西,但都没有达到我想要的效果。我看了一遍,我肯定我也做了同样的事情 import sqlite3 class Database(): def __init__(self): try: self.db = sqlite3.connect('../dat
cursor.execute(sql)
ValueError: operation parameter must be str
我尝试了很多东西,但都没有达到我想要的效果。我看了一遍,我肯定我也做了同样的事情
import sqlite3
class Database():
def __init__(self):
try:
self.db = sqlite3.connect('../database.sqlite')
self.cur = self.db.cursor()
self.cur.execute('pragma foreign_keys="1"')
except sqlite3.Error as e:
raise e
def select(self,sql):
cursor = self.db.cursor()
cursor.execute(sql)
records = cursor.fetchall()
cursor.close()
return records
def insert(self,sql):
cursor = self.db.cursor()
cursor.execute(sql)
newID = cursor.lastrowid
self.db.commit()
cursor.close()
return newID
def execute(self,sql):
""" execute any SQL statement but no return value given """
cursor = self.db.cursor()
cursor.execute(sql)
self.db.commit()
cursor.close()
if __name__ == '__main__':
db = Database()
#sql = "SELECT skuref, titre_prod FROM product"
t = ("888888",)
sql= "UPDATE product SET created = 1 WHERE skuref = ?", t
db.execute(sql)
如果有人能帮助我,我将不胜感激。后来我想在for循环的主程序中传递类似的内容
lastpost = record[0]
if created = True
sql = "UPDATE product SET created = 1 WHERE skuref = ?",(lastpost,)
db.execute(sql)
sql
是一个包含sql语句和参数的元组
更改如下,以便分别传递sql和参数,而不是作为元组传递:
def execute(self, sql):
""" execute any SQL statement but no return value given """
cursor = self.db.cursor()
cursor.execute(*sql) # <------
self.db.commit()
cursor.close()
def执行(self,sql):
“”“执行任何SQL语句,但不提供返回值”“”
cursor=self.db.cursor()
cursor.execute(*sql)#与您的语句
sql = "UPDATE product SET created = 1 WHERE skuref = ?",(lastpost,)
您已经创建了一个类似于
("UPDATE product SET created = 1 WHERE skuref = ?", (lastpost,))
必须将参数作为参数提供给execute()
函数
另外,如果您的语句不正确:否:
,=
而不是=
,则整个检查是否为True都是无效的
试试这个:
lastpost = record[0]
if created:
sql = "UPDATE product SET created = 1 WHERE skuref = ?"
db.execute(sql, lastpost)