Skip to content

Commit 3eb2b96

Browse files
gh-94628: Add explicit parameter list to sqlite3.connect docs (#94629)
Co-authored-by: CAM Gerlach <[email protected]>
1 parent bde06e1 commit 3eb2b96

File tree

1 file changed

+105
-76
lines changed

1 file changed

+105
-76
lines changed

Doc/library/sqlite3.rst

+105-76
Original file line numberDiff line numberDiff line change
@@ -246,90 +246,89 @@ Module functions and constants
246246
(bitwise or) operator.
247247

248248

249-
.. function:: connect(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri])
250-
251-
Opens a connection to the SQLite database file *database*. By default returns a
252-
:class:`Connection` object, unless a custom *factory* is given.
253-
254-
*database* is a :term:`path-like object` giving the pathname (absolute or
255-
relative to the current working directory) of the database file to be opened.
256-
You can use ``":memory:"`` to open a database connection to a database that
257-
resides in RAM instead of on disk.
258-
259-
When a database is accessed by multiple connections, and one of the processes
260-
modifies the database, the SQLite database is locked until that transaction is
261-
committed. The *timeout* parameter specifies how long the connection should wait
262-
for the lock to go away until raising an exception. The default for the timeout
263-
parameter is 5.0 (five seconds).
264-
265-
For the *isolation_level* parameter, please see the
266-
:attr:`~Connection.isolation_level` property of :class:`Connection` objects.
267-
268-
SQLite natively supports only the types TEXT, INTEGER, REAL, BLOB and NULL. If
269-
you want to use other types you must add support for them yourself. The
270-
*detect_types* parameter and using custom **converters** registered with the
271-
module-level :func:`register_converter` function allow you to easily do that.
272-
273-
*detect_types* defaults to 0 (type detection disabled).
274-
Set it to any combination (using ``|``, bitwise or) of
275-
:const:`PARSE_DECLTYPES` and :const:`PARSE_COLNAMES`
276-
to enable type detection.
277-
Column names takes precedence over declared types if both flags are set.
278-
Types cannot be detected for generated fields (for example ``max(data)``),
279-
even when the *detect_types* parameter is set.
280-
In such cases, the returned type is :class:`str`.
281-
282-
By default, *check_same_thread* is :const:`True` and only the creating thread may
283-
use the connection. If set :const:`False`, the returned connection may be shared
284-
across multiple threads. When using multiple threads with the same connection
285-
writing operations should be serialized by the user to avoid data corruption.
286-
287-
By default, the :mod:`sqlite3` module uses its :class:`Connection` class for the
288-
connect call. You can, however, subclass the :class:`Connection` class and make
289-
:func:`connect` use your class instead by providing your class for the *factory*
290-
parameter.
291-
292-
Consult the section :ref:`sqlite3-types` of this manual for details.
293-
294-
The :mod:`sqlite3` module internally uses a statement cache to avoid SQL parsing
295-
overhead. If you want to explicitly set the number of statements that are cached
296-
for the connection, you can set the *cached_statements* parameter. The currently
297-
implemented default is to cache 128 statements.
298-
299-
If *uri* is :const:`True`, *database* is interpreted as a
300-
:abbr:`URI (Uniform Resource Identifier)` with a file path and an optional
301-
query string. The scheme part *must* be ``"file:"``. The path can be a
302-
relative or absolute file path. The query string allows us to pass
303-
parameters to SQLite. Some useful URI tricks include::
304-
305-
# Open a database in read-only mode.
306-
con = sqlite3.connect("file:template.db?mode=ro", uri=True)
307-
308-
# Don't implicitly create a new database file if it does not already exist.
309-
# Will raise sqlite3.OperationalError if unable to open a database file.
310-
con = sqlite3.connect("file:nosuchdb.db?mode=rw", uri=True)
311-
312-
# Create a shared named in-memory database.
313-
con1 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
314-
con2 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
315-
con1.executescript("create table t(t); insert into t values(28);")
316-
rows = con2.execute("select * from t").fetchall()
317-
318-
More information about this feature, including a list of recognized
319-
parameters, can be found in the
320-
`SQLite URI documentation <https://www.sqlite.org/uri.html>`_.
249+
250+
.. function:: connect(database, timeout=5.0, detect_types=0, isolation_level="DEFERRED", check_same_thread=True, factory=sqlite3.Connection, cached_statements=128, uri=False)
251+
252+
Open a connection to an SQLite database.
253+
254+
:param database:
255+
The path to the database file to be opened.
256+
Pass ``":memory:"`` to open a connection to a database that is
257+
in RAM instead of on disk.
258+
:type database: :term:`path-like object`
259+
260+
:param timeout:
261+
How many seconds the connection should wait before raising
262+
an exception, if the database is locked by another connection.
263+
If another connection opens a transaction to modify the database,
264+
it will be locked until that transaction is committed.
265+
Default five seconds.
266+
:type timeout: float
267+
268+
:param detect_types:
269+
Control whether and how data types not
270+
:ref:`natively supported by SQLite <sqlite3-types>`
271+
are looked up to be converted to Python types,
272+
using the converters registered with :func:`register_converter`.
273+
Set it to any combination (using ``|``, bitwise or) of
274+
:const:`PARSE_DECLTYPES` and :const:`PARSE_COLNAMES`
275+
to enable this.
276+
Column names takes precedence over declared types if both flags are set.
277+
Types cannot be detected for generated fields (for example ``max(data)``),
278+
even when the *detect_types* parameter is set; :class:`str` will be
279+
returned instead.
280+
By default (``0``), type detection is disabled.
281+
:type detect_types: int
282+
283+
:param isolation_level:
284+
The :attr:`~Connection.isolation_level` of the connection,
285+
controlling whether and how transactions are implicitly opened.
286+
Can be ``"DEFERRED"`` (default), ``"EXCLUSIVE"`` or ``"IMMEDIATE"``;
287+
or :const:`None` to disable opening transactions implicitly.
288+
See :ref:`sqlite3-controlling-transactions` for more.
289+
:type isolation_level: str | None
290+
291+
:param check_same_thread:
292+
If :const:`True` (default), only the creating thread may use the connection.
293+
If :const:`False`, the connection may be shared across multiple threads;
294+
if so, write operations should be serialized by the user to avoid data
295+
corruption.
296+
:type check_same_thread: bool
297+
298+
:param factory:
299+
A custom subclass of :class:`Connection` to create the connection with,
300+
if not the default :class:`Connection` class.
301+
:type factory: :class:`Connection`
302+
303+
:param cached_statements:
304+
The number of statements that ``sqlite3``
305+
should internally cache for this connection, to avoid parsing overhead.
306+
By default, 128 statements.
307+
:type cached_statements: int
308+
309+
:param uri:
310+
If set to :const:`True`, *database* is interpreted as a
311+
:abbr:`URI (Uniform Resource Identifier)` with a file path
312+
and an optional query string.
313+
The scheme part *must* be ``"file:"``,
314+
and the path can be relative or absolute.
315+
The query string allows passing parameters to SQLite,
316+
enabling various :ref:`sqlite3-uri-tricks`.
317+
:type uri: bool
318+
319+
:rtype: sqlite3.Connection
321320

322321
.. audit-event:: sqlite3.connect database sqlite3.connect
323322
.. audit-event:: sqlite3.connect/handle connection_handle sqlite3.connect
324323

325-
.. versionchanged:: 3.4
326-
Added the *uri* parameter.
324+
.. versionadded:: 3.4
325+
The *uri* parameter.
327326

328327
.. versionchanged:: 3.7
329328
*database* can now also be a :term:`path-like object`, not only a string.
330329

331-
.. versionchanged:: 3.10
332-
Added the ``sqlite3.connect/handle`` auditing event.
330+
.. versionadded:: 3.10
331+
The ``sqlite3.connect/handle`` auditing event.
333332

334333

335334
.. function:: register_converter(typename, converter, /)
@@ -1480,6 +1479,36 @@ regardless of the value of :attr:`~Connection.isolation_level`.
14801479
https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
14811480

14821481

1482+
.. _sqlite3-uri-tricks:
1483+
1484+
SQLite URI tricks
1485+
-----------------
1486+
1487+
Some useful URI tricks include:
1488+
1489+
* Open a database in read-only mode::
1490+
1491+
con = sqlite3.connect("file:template.db?mode=ro", uri=True)
1492+
1493+
* Do not implicitly create a new database file if it does not already exist;
1494+
will raise :exc:`~sqlite3.OperationalError` if unable to create a new file::
1495+
1496+
con = sqlite3.connect("file:nosuchdb.db?mode=rw", uri=True)
1497+
1498+
* Create a shared named in-memory database::
1499+
1500+
con1 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1501+
con2 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1502+
con1.execute("create table t(t)")
1503+
con1.execute("insert into t values(28)")
1504+
con1.commit()
1505+
rows = con2.execute("select * from t").fetchall()
1506+
1507+
More information about this feature, including a list of parameters,
1508+
can be found in the `SQLite URI documentation`_.
1509+
1510+
.. _SQLite URI documentation: https://www.sqlite.org/uri.html
1511+
14831512
Using :mod:`sqlite3` efficiently
14841513
--------------------------------
14851514

0 commit comments

Comments
 (0)