Pythonを使用して、いくつかの選択と更新を行う必要があります。この言語ではまったく新しいものであり、次の(単純な)クエリを実行するときに構文に少し問題があります。
SELECT A.CUSTOMER_NAME,
A.CUSTOMER_CITY,
B.POPULATION
FROM
CONTRACTS AS A
JOIN CITIES AS B ON
A.CUSTOMER_CITY = B.IDENT
WHERE B.POPULATION <=500000
SELECT A.IDENT,
A.MAKE,
A.MODEL,
A.LUXURY,
B.CAR_IDENT
FROM CARS AS A
JOIN CONTRACTS AS B ON
A.IDENT = B.CAR_IDENT
WHERE LUXURY = 'Y'
UPDATE CONTRACTS
SET BASE_PRICE=1000
WHERE CONTRACT_CLASS >=10
私は更新を開始しています...コードの方が短いと思いました...更新ステートメントを実行すると、次のエラーが発生します。
>'update {} set BASE_PRICE = ? where {}'.format(self._table), (row['BASE_PRICE'], >row['where']))
>IndexError: tuple index out of range
これが私のメソッドのために持っているものです
def retrieve(self, key):
cursor = self._db.execute('select from {}'.format(self._table))
return dict(cursor.fetchone())
def update(self, row):
self._db.execute(
'update {} set BASE_PRICE = ? where {}'.format(self._table),
(row['BASE_PRICE'], row['where']))
self._db.commit()
def disp_rows(self):
cursor = self._db.execute('select IDENT, CONTRACT_CLASS, BASE_PRICE from {} order
by BASE_PRICE'.format(self._table))
for row in cursor:
print(' {}: {}: {}'.format(row['IDENT'], row['CONTRACT_CLASS'],
row['BASE_PRICE']))
def __iter__(self):
cursor = self._db.execute('select * from {} order by
BASE_PRICE'.format(self._table))
for row in cursor:
yield dict(row)
def main():
db_cities = database(filename = 'insurance.sqlite', table = 'CITIES')
db= database(filename = 'insurance.sqlite', table = 'CONTRACTS')
db_cars = database(filename = 'insurance.sqlite', table = 'CARS')
print('Retrieve rows')
for row in db: print(row)
print('Update rows')
db.update({'where': 'CONTRACT_CLASS' >= 10', 'BASE_PRICE'= 1000})
for row in db: print(row)
print('Retrieve rows after update')
for row in db: print(row)
Thanks in advance for your help!