我需要查询表,并为数据库中的每一行创建一个 Car 类的实例。我已经达到了这一点,但我收到了一个错误
return [Car(*row) for row in rows] TypeError: init () 接受 1 个位置参数,但给出了 7 个……汽车表有 6 个属性……哎呀!
import sqlite3 as lite
def db_connect():
con = lite.connect('ins.sqlite')
with con:
cur = con.cursor()
cur.execute('select * from cars;')
rows = cur.fetchall()
for row in rows:
#return (row) #Use list comprehension to get all rows
return [Car(*row) for row in rows]
class Car:
def car_info(self):
'Add all the necessary methods and properties you want'
a = db_connect()
return a
def __init__(self, **kwargs):
self.variables = kwargs
def main():
x = Car() #Create a Object x
a = x.car_info()
ok = tuple(map(str, a)) #convert float data into string
print ('Make Model Model Displacement
Power Luxury')
print (' '.join(ok))
main()