Peewee is a lightweight and simple Object-Relational Mapping (ORM) library for Python. It provides a convenient way to interact with databases and perform various operations. In this blog post, we will explore some of the commonly used functions of the Peewee library.
Connecting to a Database
The first step in using Peewee is establishing a connection to the database. Here’s an example of how to connect to a SQLite database:
import peewee
database = peewee.SqliteDatabase('database.db')
# Create the base model
class BaseModel(peewee.Model):
class Meta:
database = database
# Connect to the database
database.connect()
Replace 'database.db'
with the path to your SQLite database file, or use a different database engine if desired.
Creating Models
Peewee models represent database tables and define fields and relationships. Let’s create a simple User
model:
class User(BaseModel):
username = peewee.CharField(unique=True)
email = peewee.CharField()
age = peewee.IntegerField()
In this example, we defined three fields: username
, email
, and age
. The unique=True
attribute ensures that the username
field is unique for each user.
Creating and Saving Records
To create a new record in the database, we can simply create an instance of the model and call the save()
method:
user = User(username='john_doe', email='john@example.com', age=25)
user.save()
Peewee automatically generates SQL statements and executes them to insert the new record into the database.
Querying Records
Peewee provides a rich set of functions for querying records in the database. Here are a few examples:
Get all records
To retrieve all records from a table, we can use the select()
function:
users = User.select()
for user in users:
print(user.username, user.email, user.age)
Filter records
To filter records based on certain conditions, we can use the where()
function:
users = User.select().where(User.age >= 18) # Get users above 18 years old
for user in users:
print(user.username, user.email, user.age)
Order records
To order records by a specific field, we can use the order_by()
function:
users = User.select().order_by(User.username)
for user in users:
print(user.username, user.email, user.age)
Updating Records
To update records in the database, we can use the update()
method. Here’s an example:
user = User.get(User.username == 'john_doe')
user.email = 'john.doe@example.com'
user.save()
The get()
method retrieves a single record that matches the specified condition, and we can then update the record and save it.
Deleting Records
To delete records from the database, we can use the delete_instance()
method. Here’s an example:
user = User.get(User.username == 'john_doe')
user.delete_instance()
This will delete the specified user record from the database.
Conclusion
Peewee is a powerful and user-friendly library for working with databases in Python. It provides a wide range of functions for creating, querying, updating, and deleting records. By utilizing these functions, you can streamline your database operations and build robust applications.
For more information about Peewee and its available functions, refer to the official documentation here. Happy coding with Peewee!