[파이썬] SQLAlchemy 데이터 추가 (INSERT)

SQLAlchemy is a popular Object Relational Mapper (ORM) library for Python, which provides a convenient way to interact with databases. One common task when working with databases is inserting data into a table. In this blog post, we will learn how to use SQLAlchemy to insert data into a database table.

Setting up SQLAlchemy

Before we can start inserting data, we need to set up SQLAlchemy and connect to the database. Assuming you have already installed SQLAlchemy, you can start by importing the necessary modules and creating a database engine:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

# Replace 'database_url' with the actual database URL
engine = create_engine('database_url')
Session = sessionmaker(bind=engine)
session = Session()

Defining the table model

Next, we need to define a table model that corresponds to the table we want to insert data into. A table model is a Python class that inherits from SQLAlchemy’s Base class and represents a table in the database. It includes attributes that define the columns of the table.

Here’s an example of defining a table model for a users table with two columns - id and name:

from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    
    id = Column(Integer, primary_key=True)
    name = Column(String)

Inserting data

Once we have the table model defined, we can start inserting data into the table. Here’s how you can insert a new user into the users table:

# Create a new User object
new_user = User(name='John Doe')

# Add the new_user object to the session
session.add(new_user)

# Commit the transaction to actually insert the data into the database
session.commit()

In the above example, we first create a new User object with the desired data. Then, we add the new_user object to the session with session.add(new_user). Finally, we commit the transaction with session.commit() to persist the data into the database.

Inserting multiple rows

To insert multiple rows into a table, you can create multiple objects and add them to the session. Here’s an example:

# Create multiple User objects
users = [
    User(name='Alice'),
    User(name='Bob'),
    User(name='Carol')
]

# Add the users objects to the session
session.add_all(users)

# Commit the transaction to insert the data into the database
session.commit()

In the above example, we create a list of User objects and then use session.add_all(users) to add all the objects to the session at once. Finally, we commit the transaction to insert the data into the database.

Conclusion

With SQLAlchemy, inserting data into a database table is straightforward. By defining a table model and using the session object to add and commit the changes, you can easily insert single or multiple rows into your database.

In this blog post, we learned how to use SQLAlchemy to insert data into a database table. Remember to set up the SQLAlchemy engine and session, define the table model, and use the session to add and commit the changes.