[파이썬] Peewee 기본 CRUD (생성, 읽기, 업데이트, 삭제) 연산

Peewee is a lightweight ORM (Object Relational Mapper) for Python that provides a simple and intuitive way to interact with databases. In this blog post, we will explore the basic CRUD operations (Create, Read, Update, and Delete) using Peewee.

Installation

Before we begin, let’s make sure that Peewee is installed. Open your terminal and run the following command to install Peewee using pip:

pip install peewee

Setup

To start using Peewee, we need to first establish a connection to our database. In this example, we will be using SQLite as our database engine. Create a new python file and import the necessary packages:

from peewee import *

Next, define a database object using the SqliteDatabase class and provide the path to your database file:

db = SqliteDatabase('my_database.db')

Now, we are ready to define our models and perform CRUD operations.

Create (생성)

To create a new record in the database, we need to define a model class that extends the Model class from Peewee. The model class represents a table in the database.

Let’s say we want to create a table called User with columns name and email. Here’s an example:

class User(Model):
    name = CharField()
    email = CharField()

    class Meta:
        database = db

To create a new user, simply instantiate the model class and set the attributes:

user = User(name='John Doe', email='john.doe@example.com')
user.save()

Read (읽기)

To retrieve records from the database, we can use various methods provided by Peewee. For example, to retrieve all users from the User table, we can use the select() method:

users = User.select()
for user in users:
    print(user.name, user.email)

We can also filter records based on certain conditions. For example, to retrieve users with a specific email, we can use the where() method:

users = User.select().where(User.email == 'john.doe@example.com')
for user in users:
    print(user.name, user.email)

Update (업데이트)

To update records in the database, we can retrieve the record, modify the attributes, and call the save() method. For example, to update the email of a user with a specific name, we can do the following:

user = User.get(User.name == 'John Doe')
user.email = 'john.doe.updated@example.com'
user.save()

Delete (삭제)

To delete records from the database, we can retrieve the record and call the delete_instance() method. For example, to delete a user with a specific name, we can do the following:

user = User.get(User.name == 'John Doe')
user.delete_instance()

Conclusion

In this blog post, we explored the basic CRUD operations (Create, Read, Update, and Delete) using Peewee, a lightweight ORM for Python. We learned how to create, read, update, and delete records in the database using Peewee’s simple and intuitive API.

Peewee provides many more advanced features like joins, transactions, and querying expressions. Make sure to check out the official Peewee documentation to dive deeper into this powerful ORM. Happy coding!