On this page
sqlite3.Connection
Prev: sqlite3.connect | Up: sqlite3 | Next: sqlite3.Cursor
Rel: sqlite3.adapt, sqlite3.Binary, sqlite3.Blob, sqlite3.complete_statement
Ref: docs.python.org — sqlite3.Connection
Tags: #public #python3 #stdlib #sqlite3
Import
import sqlite3
from sqlite3 import Connection
Signature & Overview
sqlite3.Connection(...)
SQLite database connection object.
Methods & Attributes
Connection.backup(self, /, target, *, pages=-1, progress=None, name='main', sleep=0.25)— Makes a backup of the database.Connection.blobopen(self, table, column, rowid, /, *, readonly=False, name='main')— Open and return a BLOB object.Connection.close(self, /)— Close the database connection.Connection.commit(self, /)— Commit any pending transaction to the database.Connection.create_aggregate(self, /, name, n_arg, aggregate_class)— Creates a new aggregate.Connection.create_collation(self, name, callback, /)— Creates a collation function.Connection.create_function(self, /, name, narg, func, *, deterministic=False)— Creates a new function.Connection.create_window_function(self, name, num_params, aggregate_class, /)— Creates or redefines an aggregate window function. Non-standard.Connection.cursor(...)— Return a cursor for the connection.Connection.deserialize(self, data, /, *, name='main')— Load a serialized database.Connection.enable_load_extension(self, enable, /)— Enable dynamic loading of SQLite extension modules.Connection.execute(...)— Executes an SQL statement.Connection.executemany(self, sql, parameters, /)— Repeatedly executes an SQL statement.Connection.executescript(self, sql_script, /)— Executes multiple SQL statements at once.Connection.getconfig(self, op, /)— Query a boolean connection configuration option.Connection.getlimit(self, category, /)— Get connection run-time limits.Connection.interrupt(self, /)— Abort any pending database operation.Connection.iterdump(self, /, *, filter=None)— Returns iterator to the dump of the database in an SQL text format.Connection.load_extension(self, name, /, *, entrypoint=None)— Load SQLite extension module.Connection.rollback(self, /)— Roll back to the start of any pending transaction.Connection.serialize(self, /, *, name='main')— Serialize a database into a byte string.Connection.set_authorizer(self, /, authorizer_callback)— Set authorizer callback.Connection.set_progress_handler(self, /, progress_handler, n)— Set progress handler callback.Connection.set_trace_callback(self, /, trace_callback)— Set a trace callback called for each SQL statement (passed as unicode).Connection.setconfig(self, op, enable=True, /)— Set a boolean connection configuration option.Connection.setlimit(self, category, limit, /)— Set connection run-time limits.
Usage
import sqlite3
con = sqlite3.connect(":memory:")
try:
with con: # commits on success, rolls back on exception
con.execute("CREATE TABLE items (name TEXT)")
con.execute("INSERT INTO items VALUES (?)", ("widget",))
assert con.execute("SELECT name FROM items").fetchone() == ("widget",)
finally:
con.close()
Gotchas
with con:manages transactions, NOT closing: Exitingwith con:commits or rolls back, but leaves the connection open—still callcon.close()(or wrap withcontextlib.closing(con)).- Use
?placeholders: Never format SQL strings with f-strings.
REPL Inspect
from sqlite3 import Connection
help(Connection)
Linked from
Related notes
- tuts/python3/stdlib_module/sqlite3 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/connect Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/Cursor Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/adapt Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/Binary Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/Blob Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/sqlite3/complete_statement Linked here; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib