SQLAlchemy Core - Creating Table
Last Updated :
28 May, 2024
In this article, we are going to see how to create table in SQLAlchemy using Python.
SQLAlchemy is a large SQL toolkit with lots of different components. The two largest components are SQLAlchemy Core and SQLAlchemy ORM. The major difference between them is SQLAlchemy Core is a schema-centric model that means everything is treated as a part of the database i.e., rows, columns, tables, etc while SQLAlchemy ORM uses an object-centric view which encapsulates the schema with business objects. SQLAlchemy is a more pythonic implementation. In this post, we shall look at the SQLAlchemy core and how to create a table using it.
Installing SQLAlchemy
SQLAlchemy is available via the pip install package.
pip install sqlalchemy
However, if you are using a flask you can make use of its own implementation of SQLAlchemy. It can be installed using -
pip install flask-sqlalchemy
Creating Database
We are going to make use of the sqlite3 database. Follow the below process to create a database that names users:
Step 1: Open the command prompt and point to the directory to which the sqlite.exe file is present.
Step 2: Create a database named users using the command sqlite3 users.db and Check the created database using the command .databases
Creating database using sqlite3Create a table using SQLAlchemy Core
First, let us look at the entire code and then jump to the explanation and the output for the same
Python
import sqlalchemy as db
# Defining the Engine
engine = db.create_engine('sqlite:///users.db', echo=True)
# Create the Metadata Object
metadata_obj = db.MetaData()
# Define the profile table
# database name
profile = db.Table(
'profile',
metadata_obj,
db.Column('email', db.String, primary_key=True),
db.Column('name', db.String),
db.Column('contact', db.Integer),
)
# Create the profile table
metadata_obj.create_all(engine)
Output:
2021-11-08 11:08:36,988 INFO sqlalchemy.engine.base.Engine ()
2021-11-08 11:08:36,997 INFO sqlalchemy.engine.base.Engine COMMIT
Explanation:
First, we import all the requirements from the sqlalchemy library. After that, we create the engine which is used to perform all the operations like creating tables, inserting or modifying values into a table, etc. From the engine, we can create connections on which we can run database queries on. The metadata_obj contains all the information about our database which is why we pass it in when creating the table. The metadata.create_all(engine) binds the metadata to the engine and creates the profile table if it is not existing in the users database.
Output Viewed in SQLite3 terminalIn order to view the tables present in the users database, use the command .tables. In the output, when the command is used for the first time we cannot see any output that is because the above code was not run at that time. After running the above code and then using the .tables command, we can see in the sqlite3 terminal that the profile table that we created using the code is present in the users database. The SELECT query is also successfully executed which indicates the table is created. However, there is no output since we did not insert any entry in the table.
Similar Reads
SQLAlchemy Core - Multiple Tables SQLAlchemy is an open-source library that provides a set of tools for working with relational databases. It offers a high-level Object-Relational Mapping (ORM) interface as well as a lower-level SQL Expression Language (Core) interface. SQLAlchemy Core is a lightweight and flexible SQL toolkit that
4 min read
SQLAlchemy - Mapping Table Columns In this article, we will see how to map table columns using SQLAlchemy in Python. You will need a database (MySQL, PostgreSQL, SQLite, etc) to work with. Since we are going to use MySQL in this post, we will also install a SQL connector for MySQL in Python. However, none of the code implementations
5 min read
SQLAlchemy Core - Selecting Rows In this article, we are going to see how to write a query to get all rows based on certain conditions in SQLAlchemy against a PostgreSQL database in python.Creating table for demonstration:Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using
2 min read
SQLAlchemy Core - Functions SQLAlchemy provides a rich set of functions that can be used in SQL expressions to perform various operations and calculations on the data. SQLAlchemy provides the Function API to work with the SQL functions in a more flexible manner. The Function API is used to construct SQL expressions representin
7 min read
SQLAlchemy Core - Multiple Table Updates SQLAlchemy Core provides a powerful feature for performing updates on multiple tables in a database. This allows us to update related data in multiple tables in a single transaction, ensuring data consistency and integrity. In this approach, we define the tables using SQLAlchemy's Table object and c
6 min read
SQLAlchemy Core - Joins SQLAlchemy Core is a Python toolkit that enables developers to create complex database applications. It provides several features, one of which is the ability to join tables. Joining tables allows developers to retrieve data from multiple tables simultaneously, which is useful when the data is rela
3 min read
SQLAlchemy Core - Delete Statement In this article, we are going to see how to use the DELETE statement in SQLAlchemy against a PostgreSQL database in python. Creating table for demonstration: Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using create_engine() function as sh
2 min read
SQLAlchemy Core - Set Operations SQLAlchemy Core is a powerful tool for working with databases in Python. One of the key features of SQLAlchemy Core is its support for set operations, which allow you to perform complex queries on your data. In this article, we will explore the basics of set operations in SQLAlchemy Core and provide
7 min read
SQLAlchemy Core - SQL Expressions In this article, we are going to see how to write SQL Expressions using SQLAlchmey  CORE using text() in SQLAlchemy against a PostgreSQL database in Python. Creating table for demonstration Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database usin
5 min read
Column and Data Types in SQLAlchemy SQLAlchemy is an open-source library for the Python programming language that provides a set of tools for working with databases. It allows developers to interact with databases in a more Pythonic way, making it easier to write code that is both efficient and readable. Column TypesA column type in S
4 min read