Peewee – Select Records
”;
Simplest and the most obvious way to retrieve data from tables is to call select() method of corresponding model. Inside select() method, we can specify one or more field attributes. However, if none is specified, all columns are selected.
Model.select() returns a list of model instances corresponding to rows. This is similar to the result set returned by SELECT query, which can be traversed by a for loop.
from peewee import * db = SqliteDatabase(''mydatabase.db'') class User (Model): name=TextField() age=IntegerField() class Meta: database=db db_table=''User'' rows=User.select() print (rows.sql()) for row in rows: print ("name: {} age: {}".format(row.name, row.age)) db.close()
The above script displays the following output −
(''SELECT "t1"."id", "t1"."name", "t1"."age" FROM "User" AS "t1"'', []) name: Rajesh age: 21 name: Amar age : 20 name: Kiran age : 19 name: Lata age : 20
Advertisements
”;