Modules/_sqlite/
Source:
cpython 3.14 @ ab2d84fe1023/Modules/_sqlite/connection.c
_sqlite is the C extension backing the sqlite3 module. It wraps the SQLite C library for Python.
Map
| Lines | Symbol | Role |
|---|---|---|
| 1-120 | sqlite3.connect | Open or create a database file; return Connection |
| 121-350 | Connection.__init__ | Set detect_types, isolation_level, check_same_thread |
| 351-550 | Connection.cursor | Return a Cursor for the connection |
| 551-800 | Cursor.execute | Prepare and execute a SQL statement |
| 801-1000 | Cursor.fetchone / fetchmany / fetchall | Retrieve result rows |
| 1001-1300 | Row | Named-column row object |
| 1301-1800 | Adapters / converters | Python-to-SQLite type mapping |
Reading
sqlite3.connect
// CPython: Modules/_sqlite/connection.c:180 pysqlite_connection_init
static int
pysqlite_connection_init(pysqlite_Connection *self, PyObject *args,
PyObject *kwargs)
{
char *database;
int timeout = 5; /* seconds before SQLITE_BUSY error */
int detect_types = 0;
char *isolation_level = "";
int check_same_thread = 1;
/* sqlite3_open_v2 with SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE */
int rc = sqlite3_open_v2(database, &self->db, flags, NULL);
if (rc != SQLITE_OK) {
_pysqlite_seterror(state, self->db);
return -1;
}
sqlite3_busy_timeout(self->db, (int)(timeout * 1000));
...
}
detect_types=PARSE_DECLTYPES enables type converters registered with register_converter. isolation_level=None disables autocommit and uses manual transaction management.
Cursor.execute
// CPython: Modules/_sqlite/cursor.c:420 _pysqlite_query_execute
static PyObject *
_pysqlite_query_execute(pysqlite_Cursor *self, int multiple,
PyObject *args)
{
PyObject *operation;
PyObject *parameters_list = NULL;
/* sqlite3_prepare_v2: compile SQL to bytecode */
int rc = sqlite3_prepare_v2(self->db->db, sql, -1,
&self->statement->st, &tail);
/* Bind parameters */
for (int i = 0; i < num_params; i++) {
PyObject *param = PySequence_GetItem(params, i);
pysqlite_microprotocols_adapt(param, ...); /* adapter lookup */
sqlite3_bind_*(...);
}
/* Execute */
rc = pysqlite_step(self->statement->st, self->db);
...
}
execute prepares the SQL with sqlite3_prepare_v2, binds parameters (using registered adapters for custom types), and calls sqlite3_step to execute. Results are fetched lazily on fetchone/fetchall.
Row
// CPython: Modules/_sqlite/row.c:60 pysqlite_row_new
static PyObject *
pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
/* Row(cursor, data_tuple)
Stores the column description from cursor.description
and the raw data tuple. Supports name-based access:
row['column_name'], row[0], iteration, len(). */
pysqlite_Row *self = (pysqlite_Row *)type->tp_alloc(type, 0);
self->description = Py_NewRef(cursor->description);
self->data = Py_NewRef(data);
return (PyObject *)self;
}
Row supports row['name'] by scanning cursor.description for a matching column name. It is the default row_factory when sqlite3.Row is set on the connection.