Sqlalchemy Core With Text SQL For Date Range
Last Updated :
28 Apr, 2025
SQLAlchemy Core is a low-level SQL abstraction layer of SQLAlchemy, a popular Python Object Oriented Mapping(ORM) library. It provides a way to interact with relational databases wing python code, allowing developers to write SQL Queries in a more object-oriented manner.
SQLAlchemy is a python library that provides a set of tools for working with databases. It is designed to work with a wide variety of relational databases, including:
- MySQL
- PostgreSQL
- SQLite
- Oracle
- Microsoft SQL Server
It uses a unified API to interact with different database systems, allowing you to switch between databases without having to change your code. It provides a powerful and flexible way to work with SQL databases in python.
To use SQLAlchemy Core with text SQL for the date range you can use the "text" function to write a SQL Query that includes a date range filter.
Stepwise implementation:
Step 1:
Define an SQLAlchemy "engine" object to connect to your database.
Python
from sqlalchemy import create_engine
engine = create_engine('postgresql://username:password@localhost/mydatabase')
Step 2:
Create an SQLAlchemy "connection" object using the engine.
Python
connection = engine.connect()
Step 3:
Write your text SQL query with placeholders for the data range.
Here we will retrieve all the records between a start date and an end date.
SELECT *
FROM mytable
WHERE date_column >= :start_date AND date_column <= :end_date
Step 4:
Use the "text()" function from SQLAlchemy Core to create a "text" object representing your SQL query.
Python
from sqlalchemy import text
sql_query = text(
"SELECT * FROM mytable WHERE date_column >= :start_date AND date_column <= :end_date")
Step 5:
Execute the query using the "execute()" method on the connection object, passing in a dictionary with the start and end dates as values for the placeholders in the SQL query.
Python
result = connection.execute(
sql_query, {'start_date': '2022-01-01', 'end_date': '2022-12-31'})
Step 6:
Process the result set as needed, hereby iterating through the rows and converting the result set to a pandas data frame.
Python
for row in result:
print(row)
Output:
Terminal outputIn this example, we were using the "text" function to write a SQL query that selects all rows from the "table_name" table where the "date_column" column is between "start_date" and "end_date". We were then passing in the "start_date" and "end_date" parameters using the "execute" method of the SQLAlchemy "engine" object.
NOTE: When we are using SQL queries with SQLAlchemy Core, you need to be careful to properly sanitize any user input to prevent SQL injection attacks.
Similar Reads
How to count rows with SELECT COUNT(*) with SQLAlchemy? In this article, we will see how to select the count of rows using SQLAlchemy in Python. 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 changes with change in the database except for the SQL connectors
3 min read
How to Execute Raw SQL in SQLAlchemy In this article, we will see how to write a Conventional SQL query in SQLAlchemy using text() 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() func
3 min read
PostgreSQL Query To View with SQLAlchemy As a software developer, it is a common task to query a PostgreSQL view. Using views which is a virtual table representing the output of a SQL query, is considered to be an efficient way when dealing with a relational database. This article covers how to query a PostgreSQL view using SQLAlchemy in P
9 min read
SQL Query to Compare Results With Today's Date In SQL, comparing results with today's date is a powerful tool for filtering data, managing schedules, including managing tasks, appointments and performing time-sensitive analysis. By using SQL's GETDATE() function we can easily perform this comparison. The ability to filter records based on date c
4 min read
SQLAlchemy Core - Creating Table 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
3 min read
Connecting to SQL Database using SQLAlchemy in Python In this article, we will see how to connect to an SQL database using SQLAlchemy in Python. To connect to a SQL database using SQLAlchemy we will require the sqlalchemy library installed in our python environment. It can be installed using pip - !pip install sqlalchemyThe create_engine() method of sq
3 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
How to change datetime to string in SQLAlchemy query? In this article, we are going to change DateTime to string in sqlalchemy query in the python programming language. Database used: Installation Syntax to install sqlalchemy and pymysql: pip install sqlalchmey pymysql Note: pymysql is a dependency of sqlalchemy which we need to install for this post F
2 min read
Python SQLAlchemy - func.count with filter In this article, we are going to see how to perform filter operation with count function in SQLAlchemy against a PostgreSQL database in python Count with filter operations is performed in different methods using different functions. Such kinds of mathematical operations are database-dependent. In Po
3 min read
How to get specific columns in SQLAlchemy with filter? In this article, we will see how to query and select specific columns using SQLAlchemy in Python. For our examples, we have already created a Students table which we will be using: Students TableSelecting specific column in SQLAlchemy based on filter:To select specific column in SQLAlchemySyntax: sq
2 min read