Python sqlite3 Module
Example
Create an in-memory database and query data:
import sqlite3
conn = sqlite3.connect(':memory:')
cursor = conn.cursor()
cursor.execute('CREATE TABLE users (name TEXT, age INTEGER)')
cursor.execute("INSERT INTO users VALUES ('Tobias', 28)")
conn.commit()
cursor.execute('SELECT * FROM users')
result = cursor.fetchone()
print(f'User: {result[0]}, Age: {result[1]}')
Try it Yourself »
Definition and Usage
The sqlite3 module provides an interface to SQLite, a lightweight disk-based database.
Use it to create, query, and manage SQLite databases without needing a separate database server.
Members
Member | Description |
---|---|
Connection | Database connection object. |
Cursor | Database cursor object for executing SQL. |
DatabaseError | Exception for database-related errors. |
Error | Base exception class for sqlite3 errors. |
IntegrityError | Exception for integrity constraint violations. |
OperationalError | Exception for database operational errors. |
ProgrammingError | Exception for programming errors. |
Row | Row object representing a database row. |
connect() | Open a connection to a SQLite database. |
register_adapter() | Register a callable to convert custom Python types to SQLite types. |
register_converter() | Register a callable to convert SQLite types to custom Python types. |