SQLAlchemy vs. Raw SQL Queries in Python: A Compa

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

SQLAlchemy vs.

Raw SQL Queries in Python:


A Comparative Example
Python Code Nemesis · Follow
Published in Python in Plain English
5 min read · Sep 11

Listen Share

https://hotpot.ai/art-generator
Introduction
When it comes to database interaction in Python, developers often face the
decision of whether to employ SQLAlchemy, a powerful Object-Relational
Mapping (ORM) library, or opt for writing raw SQL queries. Each approach has its
unique strengths and weaknesses, and selecting the right one can significantly
impact your project’s efficiency and maintainability. In this article, we will
explore the distinctions between SQLAlchemy queries and raw SQL queries
through a comprehensive end-to-end example.
Advantages of SQLAlchemy Queries
Abstraction
SQLAlchemy offers a high-level, Pythonic interface to interact with databases,
abstracting the underlying SQL complexity. This abstraction streamlines database
interaction, reducing the necessity for database-specific code.

Readability and Maintainability


SQLAlchemy code tends to be more readable and easier to maintain than raw SQL
queries. It leverages Python classes and objects to represent database tables and
rows, making your code intuitive and self-explanatory.

Portability
With SQLAlchemy, you can seamlessly switch between various database engines
(e.g., SQLite, PostgreSQL, MySQL) by simply modifying a configuration setting.
This portability can save substantial development effort.

Security
SQLAlchemy includes built-in protection against SQL injection. It automatically
escapes user input, effectively thwarting malicious SQL code execution.

ORM Features
SQLAlchemy provides an array of features, such as database migrations,
relationship management, and query building, simplifying complex database
operations.

Disadvantages of SQLAlchemy Queries


Performance Overhead
SQLAlchemy introduces a slight performance overhead compared to raw SQL
queries. While this overhead is generally inconsequential for most applications, it
may become a concern for high-performance scenarios.

Learning Curve
SQLAlchemy has a learning curve, particularly for developers new to ORMs. It
takes some time to become proficient in SQLAlchemy’s API and concepts.
Advantages of Raw SQL Queries
Performance
Raw SQL queries can outperform ORMs for complex or optimized queries.
Developers have complete control over the SQL code, enabling fine-tuning
tailored to specific use cases.

Direct Database Interaction


Raw SQL queries allow direct interaction with the database, which is
advantageous when dealing with database-specific features or optimizations not
exposed through an ORM.

Familiarity
Developers with prior SQL experience may find raw SQL queries easier to write
and understand.

Disadvantages of Raw SQL Queries


Security Risks
Raw SQL queries are more susceptible to SQL injection attacks if not handled with
care. Developers must manually sanitize and parameterize inputs to mitigate this
risk.

Portability
Raw SQL queries are database-specific, necessitating maintenance of different
sets of SQL queries when switching databases or supporting multiple database
engines.

Readability and Maintainability


Raw SQL queries can be less readable and harder to maintain, especially for
complex queries that intertwine SQL code with Python code.
Choosing Between SQLAlchemy and Raw SQL Queries
The choice between SQLAlchemy and raw SQL queries should align with your
project’s specific requirements and trade-offs:

Use SQLAlchemy when:


Portability across different databases is essential.

Readability, maintainability, and ease of development are high priorities.

You desire built-in protection against SQL injection.

Complex ORM features are advantageous.

Use Raw SQL Queries when:


Performance optimization is paramount, and you possess expertise in writing
efficient SQL.

You are working with database-specific features or optimizations.

You have a strong familiarity with SQL.

In many cases, a hybrid approach can be effective. Use SQLAlchemy for routine
CRUD (Create, Read, Update, Delete) operations and switch to raw SQL queries for
performance-critical or complex queries.

Example: SQLAlchemy vs. Raw SQL Queries


Let’s explore these concepts through a complete code example. We will perform
basic CRUD operations for a user database using both SQLAlchemy and raw SQL
queries.

from sqlalchemy import create_engine, Column, Integer, String


from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy.exc
# SQLAlchemy setup
Base = declarative_base()
engine = create_engine('sqlite:///example.db') # Use SQLite as the database engine
# Define a User model using SQLAlchemy
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)

# Create tables
Base.metadata.create_all(engine)

# SQLAlchemy Query Example


def sqlalchemy_example():
Session = sessionmaker(bind=engine)
session = Session()
# Create a new user
new_user = User(name="Alice")
session.add(new_user)
session.commit()
# Retrieve a user
retrieved_user = session.query(User).filter_by(name="Alice").first()
print("SQLAlchemy: Retrieved User -", retrieved_user.name)
# Update user name
retrieved_user.name = "Alicia"
session.commit()
updated_user = session.query(User).filter_by(name="Alicia").first()
print("SQLAlchemy: Updated User -", updated_user.name)
session.close()

# Raw SQL Query Example


def raw_sql_query_example():
conn = engine.connect()
# Create a new user using raw SQL
try:
conn.execute("INSERT INTO users (name) VALUES ('Bob')")
except sqlalchemy.exc.IntegrityError:
print("Raw SQL: User with this name already exists")
# Retrieve a user using raw SQL
result = conn.execute("SELECT * FROM users WHERE name = 'Bob'")
retrieved_user = result.fetchone()
if retrieved_user:
print("Raw SQL: Retrieved User -", retrieved_user['name'])
# Update user name using raw SQL
conn.execute("UPDATE users SET name = 'Robert' WHERE name = 'Bob'")
updated_result = conn.execute("SELECT * FROM users WHERE name = 'Robert'"
updated_user = updated_result.fetchone()
if updated_user:
print("Raw SQL: Updated User -", updated_user['name'])
conn.close()

if __name__ == "__main__":
sqlalchemy_example() # Run the SQLAlchemy example
Open in appraw_sql_query_example() # Run the Raw SQL Query example Sign up Sign in

In this example:

We use SQLAlchemy to create a User model and interact with the database.

We also demonstrate a raw SQL query approach to achieve the same CRUD
operations.

The code showcases how SQLAlchemy abstracts database interactions,


making the code more Pythonic and readable compared to raw SQL queries.

Make sure you have the example.db file in the same directory where you run this
code to create and store the database.

Conclusion
The choice between SQLAlchemy and raw SQL queries in Python is a pivotal
decision for your project. While SQLAlchemy offers portability, security, and ease
of use, raw SQL queries provide performance and direct control over the
database. Make your choice based on your project’s specific needs, considering
factors such as performance requirements, team expertise, and the need for
36 3
cross-database support.
That’s it for this article! Feel free to leave feedback or questions in the comments.
If you found this an exciting read, leave some claps and follow! I love coffee, so
feel free to buy me a coffee; XD. Cheers!

PythonCodeNemesis writes Python Technical Articles and


Tutorials on Medium
I am a Pythonista who loves writing technical blogs and tutorials
on Medium. If you like my articles, do support me!
www.buymeacoffee.com

In Plain English
Thank you for being a part of our community! Before you go:

Be sure to clap and follow the writer!

You can find even more content at PlainEnglish.io

Sign up for our free weekly newsletter.

Follow us on Twitter(X), LinkedIn, YouTube, and Discord.

Python Database Software Development Programming Design


Follow

Written by Python Code Nemesis


490 Followers · Writer for Python in Plain English

Everything python, DSA, open source libraries and more!

More from Python Code Nemesis and Python in Plain English

Python Code Nemesis in Code Like A Girl

Creating Flowcharts with Mermaid in Python


Flowcharts are powerful visual tools for representing processes, workflows, and decision
trees. They help understand, document, and…

3 min read · Oct 2

165 4
Fateme Fouladkar in Python in Plain English

6 Types of Python Dictionaries: How to Choose the Right One!


There are actually 6 types of dictionaries in the Python standard library!

5 min read · Nov 6

768 8
Sugath Mudali in Python in Plain English

Fundamental Stock Analysis Using Python APIs


This article will look at 10 fundamental indicators for stock selection.

8 min read · Nov 7

197 4

Python Code Nemesis in Technology Hits

Understanding Webhooks in Python


Webhooks have become an essential part of modern web development and automation.
They provide a way for different web services to…

3 min read · Oct 10

133 1

See all from Python Code Nemesis

See all from Python in Plain English


Recommended from Medium

ThemeSelection in Python in Plain English

Fantastic 5 Python Build Tools Every Python Developer Should Have


Looking for the best Python build tools to accelerate your development process? Well,
check out this list of the best 5 Python build tools…

7 min read · Nov 27

96 2
Mike Huls in Towards Data Science

Python: __init__ is NOT a constructor: a deep dive in Python object


creation
Tinkering with Python’s constructor to create fast, memory-efficient classes

· 9 min read · Nov 27

421 6

Lists

General Coding Knowledge


20 stories · 653 saves

Coding & Development


11 stories · 309 saves

Stories to Help You Grow as a Software Developer


19 stories · 613 saves

Stories to Help You Grow as a Designer


11 stories · 423 saves
Anand Rathore in Towards Dev

Understanding functools in Python


Discover the power of Python’s functools module! Learn benefits, important functions, real-
life examples, and best practices for efficient…

8 min read · Nov 22

21

Python Code Nemesis in Technology Hits

Understanding Webhooks in Python


Webhooks have become an essential part of modern web development and automation.
They provide a way for different web services to…

3 min read · Oct 10


133 1

Itay

Beyond FastAPI: The Evolution of Python Microservices in 2024 with


PyNest
TL;DR: PyNest emerges as the superior framework for building APIs and microservices in
Python, addressing and resolving key issues found in…

3 min read · Nov 9

835 21
David Goudet

This is Why I Didn’t Accept You as a Senior Software Engineer


An Alarming Trend in The Software Industry

· 5 min read · Jul 25

5.1K 59

See more recommendations

You might also like