The sqlite3 Python library is a module that provides a lightweight and serverless relational database system integrated with Python. The library interfaces with SQLite version 3.7.15 and later. Here are some key points about the sqlite3 Python library:
- Embeddable: SQLite3 is an in-process database, which means it is linked into the application and runs in the same address space as the application.
- Serverless: Unlike many other database systems, SQLite3 doesn’t require a separate server to function. Its database consists of a single file on disk.
- Standard Library: The
sqlite3
module is included in the standard Python library, so you don’t need to install it separately if you’re using Python 2.5 and above. - DB-API 2.0 Compliant: The library provides an interface that’s compliant with Python’s DB-API 2.0 specification. This makes it relatively easy for developers to switch between different database systems in Python.
- Use Cases: It’s ideal for applications like embedded systems, testing, prototyping, or any scenario where a lightweight, serverless database is required.
- SQL Support:
sqlite3
allows you to execute a wide range of SQL commands, enabling you to create, modify, query, and manage relational database tables.
Table of Contents
Installation
The sqlite3
module is part of the standard library for Python 2.5 and above. No separate installation is required. However, to ensure you have the latest version:
pip install pysqlite3
Basic Commands
Creating a Database
In Python, simply connecting to a non-existent database file will create it:
import sqlite3
conn = sqlite3.connect('mydatabase.db')
Creating a Cursor:
cursor = conn.cursor()
Creating a Table:
cursor.execute('''
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
);
''')
Inserting Data:
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ('Mike', 28))
conn.commit()
Querying Data:
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
for row in rows:
print(row)
# output (1, 'Mike', 28)
Updating Data:
cursor.execute("UPDATE users SET age = ? WHERE name = ?", (30, 'Mike'))
conn.commit()
Deleting Data:
cursor.execute("DELETE FROM users WHERE name = ?", ('Mike',))
conn.commit()
Advanced Commands
Batch Inserts:
users_data = [('Peter', 25), ('Sam', 32), ('David', 29)]
cursor.executemany("INSERT INTO users (name, age) VALUES (?, ?)", users_data)
conn.commit()
Transactions:
with conn:
cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ('Diana', 27))
Row Factories:
Retrieve rows as dictionary-like objects:
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
conn.row_factory = dict_factory
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
print(cursor.fetchone()) # Outputs: {'id': 1, 'name': 'Peter', 'age': 25}
FAQ
Q: What if I want the database in memory?
A: Use sqlite3.connect(":memory:")
to create an in-memory database.
Q: How can I check if a table exists?
A: Use the following query:
SELECT name FROM sqlite_master WHERE type='table' AND name='YOUR_TABLE_NAME';
Q: Why am I seeing a Database is locked
error?
A: This error occurs if multiple writes are attempted simultaneously. Ensure that you’re not keeping transactions open longer than necessary.
Q: How do I backup my SQLite database in Python?
A: Using the backup
method:
backup_con = sqlite3.connect('backup.db')
conn.backup(backup_con)
backup_con.close()