[파이썬] SQLAlchemy Inspector 객체 활용

SQLAlchemy is a powerful Object-Relational Mapping (ORM) library for Python that provides a convenient way to interact with databases. One of the useful features of SQLAlchemy is the Inspector object, which allows you to retrieve information about an existing database.

In this blog post, we will explore how to use the SQLAlchemy Inspector object to gain insights into the structure and metadata of a database. We will cover the following topics:

  1. Installing SQLAlchemy
  2. Creating a database connection
  3. Using the Inspector object

Let’s get started!

1. Installing SQLAlchemy

Before we can use SQLAlchemy, we need to install it. You can install SQLAlchemy using pip by running the following command:

pip install SQLAlchemy

Make sure you have pip installed and configured on your system.

2. Creating a database connection

To work with an existing database, we first need to establish a connection. SQLAlchemy supports various database engines such as SQLite, MySQL, PostgreSQL, and more. Here’s an example of creating a SQLite database connection:

from sqlalchemy import create_engine

engine = create_engine('sqlite:///path/to/database.db')

connection = engine.connect()

Replace 'sqlite:///path/to/database.db' with the appropriate connection string for your database.

Once we have established a connection, we can proceed to use the Inspector object to gather database information.

3. Using the Inspector object

The Inspector object in SQLAlchemy provides various methods to inspect a database. Here are some useful ones:

3.1. get_table_names()

The get_table_names() method returns a list of all the table names in the database. Here’s an example:

from sqlalchemy import inspect

inspector = inspect(engine)

table_names = inspector.get_table_names()

print(table_names)

3.2. get_columns(table_name)

The get_columns(table_name) method retrieves the columns of a specific table. Here’s an example:

columns = inspector.get_columns('table_name')

for column in columns:
    print(column['name'], column['type'])

3.3. get_foreign_keys(table_name)

The get_foreign_keys(table_name) method returns the foreign keys of a specific table. Example usage:

foreign_keys = inspector.get_foreign_keys('table_name')

for fk in foreign_keys:
    print(fk['name'], fk['constrained_columns'], fk['referred_table'])

These are just a few examples of what you can do with the Inspector object. You can explore more methods in the SQLAlchemy documentation.

Conclusion

The SQLAlchemy Inspector object is a handy tool when working with existing databases. It allows you to retrieve information about tables, columns, and foreign keys, enabling you to gain insights into the structure and metadata of a database.

In this blog post, we covered the installation of SQLAlchemy, creating a database connection, and using the Inspector object to gather database information. Armed with this knowledge, you can now leverage SQLAlchemy to better understand and work with your databases.

Happy coding!