Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 663aa6e

Browse files
[3.10] gh-94628: Add explicit parameter list to sqlite3.connect docs (GH-94629) (#94646)
Co-authored-by: CAM Gerlach <[email protected]>. (cherry picked from commit 3eb2b96) Co-authored-by: Erlend Egeberg Aasland <[email protected]>
1 parent 7f45ae8 commit 663aa6e

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
@@ -218,90 +218,89 @@ Module functions and constants
218218
(bitwise or) operator.
219219

220220

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

294293
.. audit-event:: sqlite3.connect database sqlite3.connect
295294
.. audit-event:: sqlite3.connect/handle connection_handle sqlite3.connect
296295

297-
.. versionchanged:: 3.4
298-
Added the *uri* parameter.
296+
.. versionadded:: 3.4
297+
The *uri* parameter.
299298

300299
.. versionchanged:: 3.7
301300
*database* can now also be a :term:`path-like object`, not only a string.
302301

303-
.. versionchanged:: 3.10
304-
Added the ``sqlite3.connect/handle`` auditing event.
302+
.. versionadded:: 3.10
303+
The ``sqlite3.connect/handle`` auditing event.
305304

306305

307306
.. function:: register_converter(typename, converter, /)
@@ -1238,6 +1237,36 @@ regardless of the value of :attr:`~Connection.isolation_level`.
12381237
https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
12391238

12401239

1240+
.. _sqlite3-uri-tricks:
1241+
1242+
SQLite URI tricks
1243+
-----------------
1244+
1245+
Some useful URI tricks include:
1246+
1247+
* Open a database in read-only mode::
1248+
1249+
con = sqlite3.connect("file:template.db?mode=ro", uri=True)
1250+
1251+
* Do not implicitly create a new database file if it does not already exist;
1252+
will raise :exc:`~sqlite3.OperationalError` if unable to create a new file::
1253+
1254+
con = sqlite3.connect("file:nosuchdb.db?mode=rw", uri=True)
1255+
1256+
* Create a shared named in-memory database::
1257+
1258+
con1 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1259+
con2 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
1260+
con1.execute("create table t(t)")
1261+
con1.execute("insert into t values(28)")
1262+
con1.commit()
1263+
rows = con2.execute("select * from t").fetchall()
1264+
1265+
More information about this feature, including a list of parameters,
1266+
can be found in the `SQLite URI documentation`_.
1267+
1268+
.. _SQLite URI documentation: https://www.sqlite.org/uri.html
1269+
12411270
Using :mod:`sqlite3` efficiently
12421271
--------------------------------
12431272

0 commit comments

Comments
 (0)