Peewee Tutorial
Selected Reading
- Peewee - Discussion
- Peewee - Useful Resources
- Peewee - Quick Guide
- Peewee - Using CockroachDB
- Peewee - PostgreSQL & MySQL Extensions
- Peewee - SQLite Extensions
- Peewee - Integration with Web Frameworks
- Peewee - Query Builder
- Peewee - Database Errors
- Peewee - Atomic Transactions
- Peewee - User defined Operators
- Peewee - Retrieving Row Tuples/Dictionaries
- Peewee - SQL Functions
- Peewee - Counting & Aggregation
- Peewee - Sorting
- Peewee - Subqueries
- Peewee - Relationships & Joins
- Peewee - Connection Management
- Peewee - Defining Database Dynamically
- Peewee - Using PostgreSQL
- Peewee - Using MySQL
- Peewee - Constraints
- Peewee - Create Index
- Peewee - Delete Records
- Peewee - Update Existing Records
- Peewee - Primary & Composite Keys
- Peewee - Filters
- Peewee - Select Records
- Peewee - Insert a New Record
- Peewee - Field Class
- Peewee - Model
- Peewee - Database Class
- Peewee - Overview
- Peewee - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Peewee - Select Records
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 pst 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 = SqpteDatabase( 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 : 20Advertisements