Skip to content

sqlite3

DB-API interface for SQLite databases.

import sqlite3

Constants

Name Type Description
Row (Cursor, array[object \| None]) -> object A factory function that returns `Row` objects for query results.

Functions

sqlite3.connect(database: str) -> Connection

Open a connection to an SQLite database.

Parameters:

  • database (str) -- The path to the database file, or ":memory:" for an in-memory database.

Returns: A new Connection to the specified database.

Connection

Represents a connection to an SQLite database.

Properties

Name Type Description
row_factory ((Cursor, array[object \| None]) -> object) \| None Gets or sets the row factory used to create row objects from query results.

cursor() -> Cursor

Create a new cursor object for this connection.

Returns: A new Cursor.

execute(sql: str, parameters: system.collections.IEnumerable | None = None) -> Cursor

Create a cursor, execute a single SQL statement, and return the cursor.

Parameters:

  • sql (str) -- The SQL statement to execute.
  • parameters (system.collections.IEnumerable | None) -- Optional parameters to bind to placeholders in the SQL.

Returns: The cursor that executed the statement.

executemany(sql: str, seq_of_parameters: system.collections.IEnumerable) -> Cursor

Create a cursor and execute an SQL statement against all parameter sequences.

Parameters:

  • sql (str) -- The SQL statement to execute.
  • seq_of_parameters (system.collections.IEnumerable) -- An iterable of parameter sequences.

Returns: The cursor that executed the statements.

executescript(sql_script: str) -> Cursor

Create a cursor and execute a script of one or more SQL statements.

Parameters:

  • sql_script (str) -- A string containing one or more SQL statements separated by semicolons.

Returns: The cursor that executed the script.

commit()

Commit the current transaction.

rollback()

Roll back the current transaction.

close()

Close the connection. Any pending transaction is rolled back.

Cursor

Represents a database cursor used to execute SQL statements and fetch results.

Properties

Name Type Description
arraysize int Gets or sets the number of rows to fetch at a time with `fetchmany`. Default is 1.
lastrowid int64 Gets the row ID of the last modified row, or -1 if no row was inserted.
rowcount int Gets the number of rows affected by the last DML statement, or -1 for queries.
description list[array[object \| None]] \| None Gets column descriptions for the last query, or null if no query has been executed.

execute(sql: str, parameters: system.collections.IEnumerable | None = None) -> Cursor

Execute a single SQL statement, optionally binding parameters.

Parameters:

  • sql (str) -- The SQL statement to execute. Use ? as a placeholder for positional parameters.
  • parameters (system.collections.IEnumerable | None) -- Optional parameters to bind to placeholders in the SQL.

Returns: This cursor instance.

executemany(sql: str, seq_of_parameters: system.collections.IEnumerable) -> Cursor

Execute an SQL statement against all parameter sequences in the given iterable.

Parameters:

  • sql (str) -- The SQL statement to execute.
  • seq_of_parameters (system.collections.IEnumerable) -- An iterable of parameter sequences.

Returns: This cursor instance.

executescript(sql_script: str) -> Cursor

Execute a script of one or more SQL statements, committing any pending transaction first.

Parameters:

  • sql_script (str) -- A string containing one or more SQL statements separated by semicolons.

Returns: This cursor instance.

fetchone() -> object | None

Fetch the next row of a query result, returning None if no more data is available.

Returns: The next row as an array of values, or None.

fetchmany(size: int = -1) -> list[object]

Fetch the next set of rows, returning a list. An empty list is returned when no more rows are available.

Parameters:

  • size (int) -- The maximum number of rows to fetch. Defaults to arraysize.

Returns: A list of row objects.

fetchall() -> list[object]

Fetch all remaining rows of a query result, returning a list.

Returns: A list of all remaining row objects.

close()

Close the cursor. The cursor will be unusable from this point forward.

Error

Base exception for all sqlite3-related errors.

DatabaseError

Exception raised for errors related to the database.

OperationalError

Exception raised for errors related to the database's operation, such as SQL syntax errors.

IntegrityError

Exception raised when the relational integrity of the database is affected, such as a foreign key or uniqueness constraint violation.

ProgrammingError

Exception raised for programming errors, such as operating on a closed database or cursor.

InterfaceError

Exception raised for errors related to the database interface rather than the database itself.

Row

Represents a row returned from a query when using Sqlite3.Row as the row factory.

Properties

Name Type Description
count int Gets the number of columns in the row.

keys() -> list[str]

Return a list of column names.

Returns: A list of column name strings.

__str__() -> str

repr() uses the same method. Return a string representation of the row.