A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/introduction-to-sqlite-in-python/ below:

Introduction to SQLite in Python

Introduction to SQLite in Python

Last Updated : 15 Jul, 2025

SQLite is a lightweight, fast and embedded SQL database engine. SQLite is perfect for small to medium-sized applications, prototyping, embedded systems and local data storage in Python applications because it doesn't require a separate server process like other relational database management systems (RDBMS) like MySQL or PostgreSQL.

Features of SQLite:
  1. Serverless
  2. Self-Contained
  3. Zero-Configuration
  4. Transactional
  5. Single-Database

We don't need to install anything additional to get started because Python has built-in support for SQLite through the sqlite3 module. Let's understance each of the features in detail.

Serverless

Generally, an RDBMS such as MySQL, PostgreSQL, etc., needs a separate server process to operate. The applications that want to access the database server use TCP/IP protocol to send and receive requests and it is called client/server architecture. The diagram below illustrates the working of relational databases:

Working of Relational Databases

SQLite does not require a server to run. SQLite database read and write directly from the database files stored on disk and applications interact with that SQLite database. It is one of SQLite's biggest advantages is that it is serverless. Here's what that means:

Working of SQLite Self-Contained

SQLite is self-contained, here's what it means:

Key benefits of being Self-Contained: Zero-Configuration

Unlike other databases, SQLite requires zero setup:

Example: Connecting to a SQLite Database in Python

Python
import sqlite3

conn = sqlite3.connect('example.db')  # Creates a new database file if it doesn’t exist
cursor = conn.cursor()
Transactional

We can also manage transactions explicitly using:

Transaction Control Example:

Python
conn.execute("BEGIN")
# perform database operations
conn.commit()  # or conn.rollback() if something fails
Single-Database

SQLite uses a single-file database architecture, meaning:

Benefits of Single-File Storage:

Working of SQLite in Python Python includes built-in support for SQLite via the sqlite3 module, which conforms to the Python Database API Specification v2.0 (PEP 249). Working: Advanced Features in SQLite with Python

While SQLite is simple to use, it offers several advanced features:

Parameterized Queries

To prevent SQL injection:

cursor.execute("SELECT * FROM users WHERE id = ?", (user_id,))

Row Factory

Fetch rows as dictionaries:

conn.row_factory = sqlite3.Row

In-Memory Databases

For temporary, fast operations:

conn = sqlite3.connect(':memory:')

Using with Statement for Safe Resource Handling:

with sqlite3.connect('example.db') as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")

Explanation:

import sqlite3: imports Python’s built-in SQLite module, which allows interaction with SQLite databases.

sqlite3.connect('example.db'): establishes a connection to a SQLite database file named example.db.

conn.cursor(): creates a cursor object from the connection, which is used to execute SQL queries and fetch results.

To learn more about different types of relation databses, refer to: MySQL, PostgreSQL.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4