データベースの値を更新する関数を作成しようとしていますが、「タプルインデックスが範囲外です」というエラーが発生します。私はタプルで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': 'test1'、 'QUANTITY':5、 'ID':1、 'COST':4.99} {'NAME': 'test2'、 'QUANTITY':5、 'ID' ':2、' COST ':7.99} 費用を計算する {'費用*数量:24.950000000000003、 '名前': 'テスト1'} {'費用*数量':39.95、 '名前': 'テスト2'} ['NAME': 'test1'、 'QUANTITY':5、 'ID':1、 'COST':4.99} {'NAME': 'test2'、 'QUANTITY':5、 'ID':行を取得しています { :2、 'COST':7.99} 行を更新する トレースバック(最新の最後の呼び出し): ファイル "C:\ Users \ Wilfredo \ Documents \ Aptanaスタジオ3ワークスペース\ GroceryList \ Grocery \ dbAccess.py"、行103、 の場合の名前 == "メイン":メイン()
ファイル "C:\ Users \ Wilfredo \ Documents \ Aptana Studio 3ワークスペース\ GroceryList \ Grocery \ dbAccess.py "、行96、メイン db.updateValues(0、1) ファイル" C:\ Users \ Wilfredo \ Documents \ Aptana Studio 3ワークスペース\ GroceryList \ Grocery \ dbAccess.py "、27行目、updateValues内 self._db.execute(UPDATE {} SET QUANTITY = {} WHERE ID = {} 'フォーマット(self._table)、(quantity、ID)) IndexError:タプルインデックスのうち範囲
問題はあなたに感謝。別の質問がありますが、他の行は同じ方法で書式設定されていますが、insertValues関数のようにエラーを表示しなかったのは、updateValues関数と同じ方法で記述されていますが、実行時に働いたinsertValues関数の違いは何ですか? – wrivera24