API Reference

Connection

aiosqlite.connect(database, *, iter_chunk_size=64, loop=None, **kwargs)

Create and return a connection proxy to the sqlite database.

Parameters
  • database (Union[str, pathlib.Path]) –

  • loop (Optional[asyncio.events.AbstractEventLoop]) –

  • kwargs (Any) –

Return type

aiosqlite.core.Connection

class aiosqlite.Connection(connector, iter_chunk_size, loop=None)

Bases: threading.Thread

Parameters
  • connector (Callable[[], sqlite3.Connection]) –

  • iter_chunk_size (int) –

  • loop (Optional[asyncio.events.AbstractEventLoop]) –

Return type

None

async __aenter__()
Return type

aiosqlite.core.Connection

async __aexit__(exc_type, exc_val, exc_tb)
Return type

None

__await__()
Return type

Generator[Any, None, aiosqlite.core.Connection]

async backup(target, *, pages=0, progress=None, name='main', sleep=0.25)

Make a backup of the current database to the target database.

Takes either a standard sqlite3 or aiosqlite Connection object as the target.

Parameters
Return type

None

async close()

Complete queued queries/cursors and close the connection.

Return type

None

async commit()

Commit the current transaction.

Return type

None

async create_function(name, num_params, func, deterministic=False)

Create user-defined function that can be later used within SQL statements. Must be run within the same thread that query executions take place so instead of executing directly against the connection, we defer this to run function.

In Python 3.8 and above, if deterministic is true, the created function is marked as deterministic, which allows SQLite to perform additional optimizations. This flag is supported by SQLite 3.8.3 or higher, NotSupportedError will be raised if used with older versions.

Parameters
  • name (str) –

  • num_params (int) –

  • func (Callable) –

  • deterministic (bool) –

Return type

None

cursor()

Create an aiosqlite cursor wrapping a sqlite3 cursor object.

Return type

aiosqlite.cursor.Cursor

async enable_load_extension(value)
Parameters

value (bool) –

Return type

None

execute(sql, parameters=None)

Helper to create a cursor and execute the given query.

Parameters
  • sql (str) –

  • parameters (Iterable[Any]) –

Return type

aiosqlite.cursor.Cursor

execute_fetchall(sql, parameters=None)

Helper to execute a query and return all the data.

Parameters
  • sql (str) –

  • parameters (Iterable[Any]) –

Return type

Iterable[sqlite3.Row]

execute_insert(sql, parameters=None)

Helper to insert and get the last_insert_rowid.

Parameters
  • sql (str) –

  • parameters (Iterable[Any]) –

Return type

Optional[sqlite3.Row]

executemany(sql, parameters)

Helper to create a cursor and execute the given multiquery.

Parameters
  • sql (str) –

  • parameters (Iterable[Iterable[Any]]) –

Return type

aiosqlite.cursor.Cursor

executescript(sql_script)

Helper to create a cursor and execute a user script.

Parameters

sql_script (str) –

Return type

aiosqlite.cursor.Cursor

async interrupt()

Interrupt pending queries.

Return type

None

iterdump()

Return an async iterator to dump the database in SQL text format.

Example:

async for line in db.iterdump():
    ...
Return type

AsyncIterator[str]

async load_extension(path)
Parameters

path (str) –

async rollback()

Roll back the current transaction.

Return type

None

async set_progress_handler(handler, n)
Parameters
  • handler (Callable[[], Optional[int]]) –

  • n (int) –

Return type

None

async set_trace_callback(handler)
Parameters

handler (Callable) –

Return type

None

property in_transaction: bool
property isolation_level: str
property row_factory: Optional[Type]
property text_factory: Type
property total_changes: int

Cursors

class aiosqlite.cursor.Cursor(conn, cursor)

Bases: object

Parameters
Return type

None

async __aenter__()
async __aexit__(exc_type, exc_val, exc_tb)
__aiter__()

The cursor proxy is also an async iterator.

Return type

AsyncIterator[sqlite3.Row]

async close()

Close the cursor.

Return type

None

async execute(sql, parameters=None)

Execute the given query.

Parameters
  • sql (str) –

  • parameters (Optional[Iterable[Any]]) –

Return type

aiosqlite.cursor.Cursor

async executemany(sql, parameters)

Execute the given multiquery.

Parameters
  • sql (str) –

  • parameters (Iterable[Iterable[Any]]) –

Return type

aiosqlite.cursor.Cursor

async executescript(sql_script)

Execute a user script.

Parameters

sql_script (str) –

Return type

aiosqlite.cursor.Cursor

async fetchall()

Fetch all remaining rows.

Return type

Iterable[sqlite3.Row]

async fetchmany(size=None)

Fetch up to cursor.arraysize number of rows.

Parameters

size (Optional[int]) –

Return type

Iterable[sqlite3.Row]

async fetchone()

Fetch a single row.

Return type

Optional[sqlite3.Row]

property arraysize: int
property connection: sqlite3.Connection
property description: Tuple[Tuple]
property lastrowid: int
property rowcount: int

Errors

exception aiosqlite.Warning

Bases: Exception

exception aiosqlite.Error

Bases: Exception

exception aiosqlite.DatabaseError

Bases: sqlite3.Error

exception aiosqlite.IntegrityError

Bases: sqlite3.DatabaseError

exception aiosqlite.ProgrammingError

Bases: sqlite3.DatabaseError

exception aiosqlite.OperationalError

Bases: sqlite3.DatabaseError

exception aiosqlite.NotSupportedError

Bases: sqlite3.DatabaseError

Advanced

aiosqlite.register_adapter(type, callable)

Registers an adapter with pysqlite’s adapter registry. Non-standard.

aiosqlite.register_converter(typename, callable)

Registers a converter with pysqlite. Non-standard.