{"id":174083,"date":"2023-11-27T10:59:20","date_gmt":"2023-11-27T09:59:20","guid":{"rendered":"https:\/\/liora.io\/en\/?p=174083"},"modified":"2026-02-23T14:05:52","modified_gmt":"2026-02-23T13:05:52","slug":"sqlalchemy-what-is-it-whats-it-for","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/sqlalchemy-what-is-it-whats-it-for","title":{"rendered":"SQLAlchemy: What is it? What&#8217;s it for?"},"content":{"rendered":"\n<p><strong>How to combine the flexibility of SQL with the simplicity and efficiency of the Python language? That&#8217;s the ambition of SQLAlchemy. Discover everything you need to know about SQLAlchemy, a tool based on the object-relational mapping (ORM) principle.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-object-relational-mapping\">What is object-relational mapping?<\/h2>\n\n\n\n<p><strong>SQLAlchemy<\/strong> is a tool based on the <a href=\"https:\/\/liora.io\/en\/all-about-non-relational-databases\">object-relational mapping (ORM) principle.<\/a> ORM is a computer technique that maps the schema of a relational database (<a href=\"https:\/\/liora.io\/en\/sql-learn-all-about-the-programming-language-for-databases\">commonly known as SQL databases<\/a>) and the classes of an object-oriented programming language (in this case, Python).<\/p>\n\n\n\n<p>It creates a virtual layer between us and the database, which we can then use to query the <a href=\"https:\/\/liora.io\/en\/selenium-python-euronews-web-scraping-case-study\">database directly from a Python script.<\/a> SQLAlchemy simplifies the connection between Python and SQL databases by automatically <a href=\"https:\/\/liora.io\/en\/pytest-for-python-why-and-how-to-use-it\">converting Python<\/a> class calls into SQL statements. This makes it possible to query relational databases in a Pythonic way.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-parts-decomposition-alchemy\">Parts\/Decomposition Alchemy<\/h3>\n\n\n\n<p><strong>SQLAlchemy<\/strong> can be divided into several parts:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The engine, which is used to connect to the database and communicate our queries: <code>create_engine()<\/code>. Typically, you pass a string argument indicating the location of the database (local path or URL), the database driver\/dialect, and connection arguments (which can include credentials). In the following example, we use <strong>SQLite with a local database:\u00a0<\/strong><\/li>\n<\/ol>\n\n\n\n<p>engine = create_engine(&#8220;sqlite:\/\/\/base1.db&#8221;, echo=False)<\/p>\n\n\n\n<p>However, you could also use PostgreSQL:<\/p>\n\n\n\n<p>engine = create_engine(&#8220;postgresql:\/\/\/base1.db&#8221;, echo=False)<\/p>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><p>DBAPI stands for &#8220;<a href=\"https:\/\/liora.io\/en\/python-all-you-need-to-know\">Python Database API Specification.<\/a>&#8221; SQLAlchemy, like other database connection packages, is built on this global interface and adds its specific services on top of it.<\/p><\/li>\n\n\n\n<li><p>SQLAlchemy also allows you to manage the Data Modification Language (DML) part of SQL, which includes basic table modification operations known as CRUD operations (Create, Read, Update, Delete).<\/p><\/li>\n<\/ol>\n\n\n\n<p><strong>SQLAlchemy<\/strong> maps <strong>SQL INSERT, UPDATE, and DELETE<\/strong> statements to its own methods: <code>insert<\/code>, <code>update<\/code>, and <code>delete<\/code>, thanks to the ORM mechanism. This allows you to modify tables in an object-oriented way, as shown in the following example.<\/p>\n\n\n\n<p>We connect to the database via .connect(), then give SQL statements (INSERT to create a row, <a href=\"https:\/\/liora.io\/en\/sql-delete-how-do-i-use-this-query\">DELETE to delete it,<\/a> SELECT for a simple read) followed by .execute() to send them to the database.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Metadata<\/strong> and strict table schema are common features of relational databases.<\/li>\n\n\n\n<li><strong>SQL alchemy<\/strong> provides predefined classes to facilitate Data Definition Language (DDL), i.e. the part of SQL concerned with the creation of relational database schemas (type, keys, etc.).<\/li>\n\n\n\n<li>For example, the <strong>Column<\/strong> class represents a field in the table, Integer specifies that the field is of type &#8220;INT&#8221;, and String that it is a &#8220;varchar&#8221;.<\/li>\n\n\n\n<li>The <strong>ForeignKey<\/strong> class specifies that the column is a foreign key. Note that all these classes inherit from a global class called <strong>MetaData.<\/strong><\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\" style=\"margin-top:var(--wp--preset--spacing--columns);margin-bottom:var(--wp--preset--spacing--columns)\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\">Learn how to use SQLAlchemy<\/a><\/div>\n<\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In this second example, we create a new Locations table containing locations in new regions, and <a href=\"https:\/\/liora.io\/en\/crud-definition-and-operation\">perform CRUD operations on it.<\/a><\/li>\n\n\n\n<li>To this end, <strong>SQLAlchemy<\/strong> provides us with pre-mastered classes such as Column or Table.<\/li>\n\n\n\n<li>Note that <a href=\"https:\/\/liora.io\/en\/sql-insert-into-insert-data-with-this-sql-query\">Python functions such as .insert()<\/a> , update, .where or delete are used instead of SQL.<\/li>\n\n\n\n<li>However, the syntax of elementary functions such as engine, .connect(),execute may put some people off, as you have to provide your own SQL code to interact with your database.<\/li>\n\n\n\n<li>A session allows us to perform the same operations (read, write and delete operations) using more easily manipulated Python objects, which are then mapped to the database.<\/li>\n\n\n\n<li>Sessions are therefore used to <strong>perform object relationship management (ORM)<\/strong>, and also keep track of objects that have been modified during the session.<\/li>\n<\/ul>\n\n\n\n<p>An example to illustrate this more practical interface:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We<strong> carry out the same operations as before,<\/strong> but we notice that the syntax is much lighter: sessionmaker is used to define our session, which allows us to use only Python methods to communicate instructions to the database via a Python class, in this case Pokemon, which uses the pokedex table via the __tablename__ parameter.<\/li>\n\n\n\n<li>We can then retrieve information from the database via the query() method, filter the results with filter_by() and then delete the object from the session with the delete() method.<\/li>\n\n\n\n<li>Finally, let&#8217;s <strong>illustrate the notion of upsert , at the end of this last example.<\/strong><\/li>\n\n\n\n<li>We want to add several pok\u00e9mons to our database, but we don&#8217;t know whether it already contains some or not.<\/li>\n\n\n\n<li>To avoid adding duplicates, we&#8217;ll use the merge() function, which will update the information on pok\u00e9mons already in the database (update) or insert a new row in the table (insert).<\/li>\n\n\n\n<li>This combination of update and insert is called upsert.<\/li>\n<\/ul>\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-sqlalchemys-uses\">What are SQLAlchemy&#8217;s uses?<\/h2>\n\n\n\n<p>ORM, and more generally <strong>SQLAlchemy,<\/strong> is suitable for the following situations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>basic use of <strong>CRUD operations<\/strong><\/li>\n\n\n\n<li>Writing better tests and data fixtures for data consistency,<\/li>\n\n\n\n<li><strong>SQLAlchemy<\/strong> is DataBase agnostic. This means that our python code will be the same for all SQL environments and dialects such as SQlite, PostGReSQL or Oracle.<\/li>\n\n\n\n<li>This improves interoperability with the rest of your application, so you can change database systems without having to change your code.<\/li>\n\n\n\n<li>Instead of juggling between different SQL dialects, the open source <strong>SQL Alchemy<\/strong> toolkit allows us to streamline our workflow and process our data efficiently from within the Python language.<\/li>\n\n\n\n<li>Writing table schema constraints directly from our Python script.<\/li>\n\n\n\n<li>Representing relational databases in Python classes not only improves code quality and avoids duplication, making maintenance easier, but also enables objects to be stored in a memory cache.<\/li>\n\n\n\n<li>This speeds up data access by reducing the number of queries, and increases performance.<\/li>\n\n\n\n<li>If you don&#8217;t have a cloud-hosted database such as a <a href=\"https:\/\/liora.io\/en\/data-warehouse-2\">Datawarehouse<\/a>, you can use the open-source PostGreSQL database via SQLAlchemy.<\/li>\n<\/ul>\n\n\n\n<p>It should be noted, however, that ORM is ill-suited to the processing of voluminous data, as well as to <a href=\"https:\/\/liora.io\/en\/etl-or-extract-transform-load-definition-and-use\">ETL processes.<\/a> In the latter case, dbt would be more appropriate (packages, macros, tests).<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>Are you passionate about SQL, Python and the world of data, and want to discover essential tools like dbt, Spark or SQLAlchemy?<\/p>\n\n\n\n<p>Join our Data Analyst, Data Scientist or Analytics Engineer training courses at Liora to <a href=\"https:\/\/liora.io\/en\/big-data-training-everything-you-need-to-know\">perfect your knowledge of the subject via the Big Data sprint courses.ig<\/a><\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\">Find out more about our training courses<\/a><\/div>\n<\/div>\n\n\n\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [\n    {\n      \"@type\": \"Question\",\n      \"name\": \"What is SQLAlchemy?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"SQLAlchemy is a Python SQL toolkit and Object Relational Mapper (ORM) that provides a full suite of tools for working with relational databases in Python applications.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"How does SQLAlchemy work?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"SQLAlchemy works by offering two main components: the Core for executing raw SQL expressions and the ORM for mapping Python classes to database tables, enabling interaction with databases using Python objects.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"What is the difference between SQLAlchemy Core and ORM?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"SQLAlchemy Core provides a low-level SQL expression language for database interaction, while the ORM allows developers to work with databases using object-oriented programming concepts.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"Why use SQLAlchemy?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"SQLAlchemy is used to simplify database interactions in Python, support multiple database systems, improve code maintainability, and provide flexibility between raw SQL and ORM-based approaches.\"\n      }\n    }\n  ]\n}\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>How to combine the flexibility of SQL with the simplicity and efficiency of the Python language? That&#8217;s the ambition of SQLAlchemy. Discover everything you need to know about SQLAlchemy, a tool based on the object-relational mapping (ORM) principle. What is object-relational mapping? SQLAlchemy is a tool based on the object-relational mapping (ORM) principle. ORM is [&hellip;]<\/p>\n","protected":false},"author":82,"featured_media":207898,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2434],"class_list":["post-174083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-dev"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/174083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/82"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=174083"}],"version-history":[{"count":4,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/174083\/revisions"}],"predecessor-version":[{"id":207900,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/174083\/revisions\/207900"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/207898"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=174083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=174083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}