def cursor2cars(cursor): cars = [] for row in cursor:# each row is a tuple c = Car(brand=row[1],model=row[2],price=row[3]) c.id = row[0] # we don't need to cast type, it has already to cast to correct type cars.append(c) return cars
def cursor2cars(cursor): cars = [] for row in cursor: # each row is a tuple c = Car(brand=row[1], model=row[2], price=row[3]) c.id = row[ 0] # we don't need to cast type, it has already to cast to correct type cars.append(c) return cars
def load_access_col_by_name(): """ to access each column in row by name instead of index we need to set connection's row_factory to 'sqlite3.Row' """ with sqlite3.connect(Database) as conn: conn.text_factory = str # by default, it will return unicode, so change it to 'str' conn.row_factory = sqlite3.Row cursor = conn.execute("SELECT * FROM Cars") cars = [] for row in cursor: c = Car(brand=row["Brand"],price=row["Price"],model=row["Model"]) c.id = row["Id"] cars.append(c) Car.display(cars)
def load_access_col_by_name(): """ to access each column in row by name instead of index we need to set connection's row_factory to 'sqlite3.Row' """ with sqlite3.connect(Database) as conn: conn.text_factory = str # by default, it will return unicode, so change it to 'str' conn.row_factory = sqlite3.Row cursor = conn.execute("SELECT * FROM Cars") cars = [] for row in cursor: c = Car(brand=row["Brand"], price=row["Price"], model=row["Model"]) c.id = row["Id"] cars.append(c) Car.display(cars)