![]() |
VOOZH | about |
SQLAlchemy Core and ORM are two different components of the SQLAlchemy library in Python. The main difference between the two is the level of abstraction they provide when working with SQL databases:
In summary, SQLAlchemy Core is for those who need a low-level and flexible SQL toolkit, while SQLAlchemy ORM is for those who want a more Pythonic and abstract way of working with databases. Let's understand it in more detail
SQLAlchemy Core is a low-level SQL toolkit provided as part of the SQLAlchemy library in Python. It provides a SQL abstraction layer, allowing you to work with SQL databases in a more Pythonic way, while still retaining full control over the SQL being executed. It is designed to be flexible, fast, and compatible with a wide range of databases, including MySQL, PostgreSQL, and SQLite.
This example shows how you can use SQLAlchemy Core to create a connection to an SQLite database, execute a SELECT statement, retrieve the results, and loop through the rows. The text function is used to create a SQL expression, which is then passed to the execute method of the engine object. The fetchall method retrieves all rows from the result set as a list of dictionaries, where each dictionary represents a row and the keys are the column names.
SQLAlchemy ORM (Object Relational Mapper) is a higher-level API built on top of SQLAlchemy Core, providing an easier way to interact with databases using Python classes and objects. It allows you to map Python classes to database tables, and to interact with the data in those tables using instances of those classes. This can simplify database operations and allow you to write more Pythonic code. The ORM also provides a range of advanced features such as lazy loading, caching, and transactional control, making it a powerful tool for managing database-driven applications.
This example demonstrates how you can use SQLAlchemy ORM to define a database model, create a connection to an SQLite database, insert data, and retrieve data. The declarative_base class is used to create a base class for the model classes, and the Column class is used to define the columns in the table. The session maker function is used to create a factory for creating database sessions and session.add_all method is used to add multiple instances of the User model to the session. The session.commit method is used to persist the changes to the database and the session.query method is used to create a query for retrieving data from the Users from the table.
Output:
id=1, name='Alice', age=30 id=2, name='Bob', age=35 id=3, name='Charlie', age=40
Core | ORM | |
|---|---|---|
Level of Abstraction | The Core is a low-level API that allows developers to interact directly with the database using SQL, while the ORM is a high-level API that allows developers to interact with the database using Python objects. This means that the Core provides more fine-grained control over the database and allows for more complex queries, but it also requires developers to be familiar with SQL and may require more code to be written | The ORM provides a more intuitive and Pythonic way of working with the data, but it also has a higher overhead and may not be as performant as using the Core. |
The trade off between convenience and performance | The Core provides more fine-grained control over the database and allows for more complex queries, but it requires developers to be familiar with SQL and may require more code to be written. | The ORM provides a lot of convenience and abstraction, making it easier to reason about the data and application logic, but it also has a higher overhead and may not be as performant as using the Core. |
Complex Queries | The Core allows for more complex queries and fine-tuning of the performance by allowing developers to write raw SQL statements and has fine-grained control over the database. | The ORM is more suitable for CRUD operations and simple queries. |
Familiarity with SQL | The Core requires more familiarity with SQL and may require more code to be written. | The ORM abstracts away the need to write raw SQL statements and is more accessible to developers who are not familiar with SQL. |
Mapping | The Core does not handle mapping and developers would need to manually map the data. | The ORM also automatically handles mapping between the Python objects and the database rows, adding convenience to the development process and reducing the amount of boilerplate code that needs to be written |
Transactions and connection pooling | The Core does not provide built-in support for these features, developers need to handle these aspects manually. | The ORM provides built-in support for transactions and connection pooling, making it more convenient for developers to manage these aspects of the database interactions. |