Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. Python 3.11, SQLite
  4. Executing Raw SQL Queries in Python with SQLite: A Comprehensive Guide

Executing Raw SQL Queries in Python with SQLite: A Comprehensive Guide

Date- Apr 11,2026 13
python sqlite

Overview

SQLite is a lightweight, serverless, self-contained SQL database engine that is widely used for local data storage in applications. One of its key features is the ability to execute raw SQL queries directly, which provides developers with the flexibility to perform complex database operations tailored to specific needs. This capability is particularly valuable in scenarios where the ORM (Object Relational Mapping) abstractions may not cover advanced use cases, such as complex joins or performance optimizations.

In real-world applications, executing raw SQL can be beneficial in various contexts such as data analysis, reporting, and batch processing where performance is critical. For example, data scientists may need to run complex analytical queries against large datasets stored in SQLite databases, while web applications might require dynamic SQL execution based on user input. This makes mastering raw SQL execution in Python not just a useful skill, but often a necessary one.

Prerequisites

  • Python 3.11: Ensure you have Python 3.11 installed on your system.
  • SQLite3 Module: This module comes pre-installed with Python, enabling easy database interactions.
  • Basic SQL Knowledge: Familiarity with SQL syntax is crucial for crafting effective queries.
  • Text Editor or IDE: A code editor like VSCode or PyCharm is recommended for writing Python scripts.

Setting Up SQLite in Python

To start executing raw SQL queries, you need to set up a connection to your SQLite database. The sqlite3 module allows you to create and manage SQLite databases directly from your Python code. Establishing a connection is the first step in executing any SQL command.

import sqlite3

def create_connection(db_file):
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        print("Connection established.")
    except sqlite3.Error as e:
        print(f"Error: {e}")
    return conn

database = "example.db"
connection = create_connection(database)

This code imports the sqlite3 module and defines a function to create a database connection. It attempts to connect to the specified database file and prints a message upon successful connection.

Expected output upon running the code is:

Connection established.

Creating a Database and Table

Once the connection is established, you can create a database table where you can store data. Creating tables is fundamental in SQL as it defines the structure of your data.

def create_table(conn):
    try:
        sql_create_table = '''CREATE TABLE IF NOT EXISTS users (
                                id INTEGER PRIMARY KEY,
                                name TEXT NOT NULL,
                                age INTEGER
                            );''' 
        cursor = conn.cursor()
        cursor.execute(sql_create_table)
        print("Table created successfully.")
    except sqlite3.Error as e:
        print(f"Error: {e}")

create_table(connection)

This function defines a SQL command to create a table named users with three columns: id, name, and age. It uses a cursor to execute the command and prints a success message if successful.

Executing Basic CRUD Operations

CRUD stands for Create, Read, Update, and Delete โ€” the four basic operations for managing data in a database. Each operation can be performed using raw SQL commands executed through Python.

Create: Inserting Data

To insert data into your database, you can use an INSERT statement. This operation is fundamental for adding new records to your tables.

def insert_user(conn, user):
    sql_insert = '''INSERT INTO users(name, age) VALUES(?, ?);'''
    cursor = conn.cursor()
    cursor.execute(sql_insert, user)
    conn.commit()
    print("User added successfully.")

insert_user(connection, ('Alice', 30))

This code defines a function to insert a user into the users table. It uses parameter substitution to avoid SQL injection vulnerabilities and commits the transaction to ensure data is saved.

Read: Querying Data

Reading data from your database can be achieved using a SELECT statement. This allows you to retrieve records based on specific criteria.

def select_all_users(conn):
    sql_select = '''SELECT * FROM users;'''
    cursor = conn.cursor()
    cursor.execute(sql_select)
    return cursor.fetchall()

users = select_all_users(connection)
print(users)

This function retrieves all user records from the users table and returns them as a list of tuples. The output will display all the records in the table.

Update: Modifying Data

Updating existing records can be done using the UPDATE statement. This is important for maintaining accurate and current data.

def update_user_age(conn, user_id, new_age):
    sql_update = '''UPDATE users SET age = ? WHERE id = ?;'''
    cursor = conn.cursor()
    cursor.execute(sql_update, (new_age, user_id))
    conn.commit()
    print("User age updated successfully.")

update_user_age(connection, 1, 31)

This function updates the age of a user based on their ID. Similar to the insert operation, it uses parameter substitution to prevent SQL injection.

Delete: Removing Data

Deleting records is performed using the DELETE statement. This operation is crucial for data management when records are no longer needed.

def delete_user(conn, user_id):
    sql_delete = '''DELETE FROM users WHERE id = ?;'''
    cursor = conn.cursor()
    cursor.execute(sql_delete, (user_id,))
    conn.commit()
    print("User deleted successfully.")

delete_user(connection, 1)

This code defines a function to delete a user by their ID. The operation is committed to the database to ensure changes are saved.

Transactions and Error Handling

Transactions are essential in SQL for ensuring data integrity. They allow you to group multiple SQL operations into a single unit of work, which can be committed or rolled back as needed.

In Python's sqlite3 module, you can manage transactions using commit and rollback methods. Error handling is also crucial for managing exceptions that can arise during database operations.

def transactional_insert(conn, user):
    try:
        cursor = conn.cursor()
        sql_insert = '''INSERT INTO users(name, age) VALUES(?, ?);'''
        cursor.execute(sql_insert, user)
        conn.commit()
        print("User added successfully.")
    except sqlite3.Error as e:
        conn.rollback()
        print(f"Transaction failed: {e}")

transactional_insert(connection, ('Bob', 25))

This function attempts to insert a user and commits the transaction if successful. If an error occurs, it rolls back any changes, ensuring the database remains in a consistent state.

Edge Cases & Gotchas

When executing raw SQL queries in Python, there are several pitfalls to be aware of. One common issue is SQL injection, which occurs when user input is directly included in SQL statements.

def unsafe_insert_user(conn, user_name, user_age):
    sql_insert = f'''INSERT INTO users(name, age) VALUES('{user_name}', {user_age});'''
    cursor = conn.cursor()
    cursor.execute(sql_insert)
    conn.commit()

This approach is unsafe as it directly embeds user input into the SQL string. A malicious user could input SQL code that alters the query's behavior, leading to data breaches.

Instead, always use parameterized queries, as shown in previous examples, to mitigate these risks.

Performance & Best Practices

When working with SQLite and executing raw SQL, there are several best practices to enhance performance and maintainability. For example, using parameterized queries not only protects against SQL injection but also can improve performance by allowing SQLite to reuse cached query plans.

Batch inserts can significantly reduce the number of commits, which is a costly operation. Instead of inserting records one by one, you can insert multiple records in a single transaction.

def batch_insert_users(conn, users):
    try:
        cursor = conn.cursor()
        sql_insert = '''INSERT INTO users(name, age) VALUES(?, ?);'''
        cursor.executemany(sql_insert, users)
        conn.commit()
        print("Batch insert completed successfully.")
    except sqlite3.Error as e:
        conn.rollback()
        print(f"Batch insert failed: {e}")

batch_insert_users(connection, [('Charlie', 35), ('Diana', 28)])

This method uses executemany to insert multiple records efficiently. Expect the output to confirm successful batch insertion.

Real-World Scenario: User Management System

To tie all these concepts together, let's create a simple User Management System that allows adding, viewing, updating, and deleting users in an SQLite database. This mini-project will use all the previously discussed operations in a cohesive manner.

def main():
    database = "users.db"
    connection = create_connection(database)
    create_table(connection)

    # Adding users
    insert_user(connection, ('Eve', 22))
    insert_user(connection, ('Frank', 29))

    # Viewing users
    users = select_all_users(connection)
    print("Current users:", users)

    # Updating a user
    update_user_age(connection, 2, 30)

    # Deleting a user
    delete_user(connection, 1)
    print("Users after deletion:", select_all_users(connection))

    connection.close()

if __name__ == '__main__':
    main()

This main function orchestrates the creation of the database, table, and performs various operations on user records. It provides a complete flow from adding users to viewing and modifying them.

Conclusion

  • Mastering raw SQL execution in Python with SQLite enables greater control over database operations.
  • Always use parameterized queries to prevent SQL injection vulnerabilities.
  • Batch operations can significantly enhance performance.
  • Transactions ensure data integrity during multiple operations.
  • Understanding edge cases and best practices is crucial for robust database management.

S
Shubham Saini
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Connecting Python 3.11 to SQL Databases Using SQLAlchemy: A Comprehensive Guide
Apr 09, 2026
Best Practices for Using SQL with Python: Performance Tips and Techniques
Apr 10, 2026
Mastering Inheritance and Polymorphism in Python: A Comprehensive Guide
Mar 27, 2026
Mastering Functions in Python: A Deep Dive into Concepts and Best Practices
Mar 26, 2026
Buy me a pizza

Comments

On this page

๐ŸŽฏ

Interview Prep

Ace your Python 3.11, SQLite interview with curated Q&As for all levels.

View Python 3.11, SQLite Interview Q&As

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor