pysqlite3
pysqlite3 is a Python library that provides a DB-API 2.0 compliant interface for SQLite 3.x databases. It effectively takes the `sqlite3` module from the Python standard library and packages it separately, often with a more recent, statically compiled SQLite library that includes additional features not always present in system-bundled SQLite versions. The current version is 0.6.0. It offers a self-contained binary distribution (`pysqlite3-binary`) that requires no external dependencies.
Warnings
- gotcha SQLite's transactional model (and thus `pysqlite3`'s) implicitly commits open transactions before Data Definition Language (DDL) statements (e.g., CREATE TABLE, ALTER TABLE, DROP TABLE, VACUUM, PRAGMA). This can lead to unexpected behavior if you expect DDL statements to be part of a larger, explicit transaction that can be rolled back. Ensure you commit or rollback explicitly before DDL if transaction integrity is critical.
- gotcha SQLite is designed for single-process access, though it handles concurrent reads well. Concurrent *writes* from multiple processes or threads can lead to 'database is locked' errors or contention. While `pysqlite3` allows specifying a timeout for locks, it does not fundamentally change SQLite's locking model.
- gotcha Always use parameter substitution (e.g., `?` placeholders) when executing SQL queries with user-provided data, rather than Python string formatting. Failure to do so exposes your application to SQL injection vulnerabilities.
- gotcha Improper application shutdowns, especially during active transactions, can lead to data corruption in SQLite databases. Ensure that all transactions are properly committed or rolled back before the application terminates.
- gotcha The `pysqlite3-binary` package provides a statically-linked, feature-rich, and up-to-date SQLite. If you install `pysqlite3` without the `-binary` suffix, it attempts to link against your system's `libsqlite3`, which might be older or lack features. Mixing these installations or expecting features only present in the binary version when using the system-linked one can lead to confusion or runtime errors.
Install
-
pip install pysqlite3-binary -
pip install pysqlite3
Imports
- pysqlite3
__import__('pysqlite3'); import sys; sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')import pysqlite3
Quickstart
import pysqlite3
# Connect to an in-memory database
conn = pysqlite3.connect(':memory:')
cursor = conn.cursor()
# Create a table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL
)
''')
conn.commit()
# Insert data
try:
cursor.execute("INSERT INTO users (name, email) VALUES (?, ?)", ('Alice', 'alice@example.com'))
cursor.execute("INSERT INTO users (name, email) VALUES (?, ?)", ('Bob', 'bob@example.com'))
conn.commit()
print('Data inserted successfully.')
except pysqlite3.IntegrityError as e:
print(f'Error inserting data: {e}')
conn.rollback()
# Query data
cursor.execute("SELECT id, name, email FROM users")
rows = cursor.fetchall()
print('\nUsers:')
for row in rows:
print(f'ID: {row[0]}, Name: {row[1]}, Email: {row[2]}')
# Close the connection
conn.close()