Python 元组索引超出数据库范围

Python 元组索引超出数据库范围,python,python-3.x,sqlite,Python,Python 3.x,Sqlite,我试图编写一个函数来更新数据库中的值,但它给了我一个“元组索引超出范围”错误。我在sql语句中的占位符数量与元组中的占位符数量相同,但它仍然会给我相同的错误。错误在第27行的updateValues函数中。我将非常感谢您的帮助,我对python和编程本身还不熟悉,我正在尝试和错误中学习 这是该类的代码: import sqlite3 class dbAccess(): def __init__(self, **kwargs): self.filename = kwargs.get('

我试图编写一个函数来更新数据库中的值,但它给了我一个“元组索引超出范围”错误。我在sql语句中的占位符数量与元组中的占位符数量相同,但它仍然会给我相同的错误。错误在第27行的updateValues函数中。我将非常感谢您的帮助,我对python和编程本身还不熟悉,我正在尝试和错误中学习

这是该类的代码:

import sqlite3

class dbAccess():

def __init__(self, **kwargs):
    self.filename = kwargs.get('filename')
    self.table = kwargs.get('table', 'grocery')

def sqlDo(self, sql, *params):
    self._db.execute(sql, params)
    self._db.commit()

def insertValues(self, db, item, cost, quantity):   
    self._db.execute('INSERT INTO {} (NAME, COST, QUANTITY) VALUES(?, ?, ?)'.format(self._table), (item, cost, quantity))
    self._db.commit()

def retrieveValues(self, ID):
    cursor = self._db.execute('select * from {} where name = ?'.format(self._table), (ID,))
    return dict(cursor.fetchone())

def updateValues(self, quantity, ID):
    self._db.execute('UPDATE {} SET QUANTITY = {} WHERE ID = {}'.format(self._table), (quantity, ID))
    self._db.commit()

def delete(self, ID):
    self._db.execute('delete from {} where t1 = ?'.format(self._table), (ID,))
    self._db.commit()

def calculateCost(self):
    cursor = self._db.execute('select name, cost * quantity from {}'.format(self._table))
    for v in cursor:
        print(dict(v))

def __iter__(self):
    cursor = self._db.execute('SELECT * FROM {} ORDER BY ID'.format(self._table))
    for row in cursor:
        yield(dict(row))


@property
def filename(self): return self._filename

@filename.setter
def filename(self, fn):
    self._filename = fn
    self._db = sqlite3.connect(fn)
    self._db.row_factory = sqlite3.Row  

@filename.deleter
def filename(self):
    self._filename.close()

@property
def table(self):
    return self._table
@table.setter
def table(self, t):
    self._table = t
@table.getter
def table(self):
    self._table = 'test'

def close(self):
    self._db.close()
    del self._filename


def main():
db = dbAccess(filename = 'test.db', table = 'test')

print('Create table test')
db.sqlDo('drop table if exists test')
db.sqlDo('''CREATE TABLE IF NOT EXISTS test(
                    ID INTEGER PRIMARY KEY AUTOINCREMENT, 
                    NAME TEXT, 
                    COST FLOAT, 
                    QUANTITY INTEGER)''')

print('Create rows')
db.insertValues('test', 'test1', 4.99, 5)
db.insertValues('test', 'test2', 7.99, 5)
for row in db: print(row)

print('Calculate Costs')
print(db.calculateCost())

print('Retrieve rows')
print(db.retrieveValues('test1'), db.retrieveValues('test2'))

print('Update rows')
db.updateValues(0, 1)
for row in db: print(row)

print('Delete rows')
db.delete('1')
for row in db: print(row)
如果name==“main”:main()

这是输出:

创建表测试 创建行 {'NAME':'test1','QUANTITY':5,'ID':1,'COST':4.99} {'NAME':'test2','QUANTITY':5,'ID':2,'COST':7.99} 计算成本 {'cost*quantity':24950000000000003,'NAME':'test1'} {'cost*quantity':39.95,'NAME':'test2'} 没有一个 检索行 {'NAME':'test1','QUANTITY':5,'ID':1,'COST':4.99}{'NAME':'test2','QUANTITY':5,'ID':2,'COST':7.99} 更新行 回溯(最近一次呼叫最后一次): 文件“C:\Users\Wilfredo\Documents\Aptana Studio 3 Workspace\GroceryList\Grocery\dbAccess.py”,第103行,在 如果name==“main”:main()
文件“C:\Users\Wilfredo\Documents\Aptana Studio 3 Workspace\GroceryList\Grocery\dbAccess.py”,第96行,主目录 db.updateValue(0,1) 文件“C:\Users\Wilfredo\Documents\Aptana Studio 3 Workspace\GroceryList\Grocery\dbAccess.py”,第27行,在UpdateValue中 self._db.execute('UPDATE{}SET QUANTITY={},其中ID={}'。格式(self._table),(QUANTITY,ID))
索引器错误:元组索引超出范围

未正确传递格式参数。例如,请看一条有问题的线:

 self._db.execute('UPDATE {} SET QUANTITY = {} WHERE ID = {}'.format(self._table), (quantity, ID))
对于类似的行,您将有其他错误

您应该这样设置格式:

self._db.execute('UPDATE {} SET QUANTITY = {} WHERE ID = {}'.format(self._table, quantity, ID)

问题解决了,谢谢。我还有一个问题,其他行的格式与updateValues函数相同,但没有出现错误,比如insertValues函数的编写方式与updateValues函数相同,但工作正常。运行时运行的insertValues函数有什么不同?