Skip to content

Commit f04bf42

Browse files
author
Erlend E. Aasland
committed
bpo-46402: Promote sqlite3 URI tricks in docs
1 parent 62a6594 commit f04bf42

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

Doc/library/sqlite3.rst

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,28 @@ Module functions and constants
273273
for the connection, you can set the *cached_statements* parameter. The currently
274274
implemented default is to cache 128 statements.
275275

276-
If *uri* is true, *database* is interpreted as a URI. This allows you
277-
to specify options. For example, to open a database in read-only mode
278-
you can use::
279-
280-
db = sqlite3.connect('file:path/to/database?mode=ro', uri=True)
281-
282-
More information about this feature, including a list of recognized options, can
283-
be found in the `SQLite URI documentation <https://www.sqlite.org/uri.html>`_.
276+
If *uri* is :const:`True`, *database* is interpreted as a
277+
:abbr:`URI (Uniform Resource Identifier)` consisting of a scheme, an
278+
authority, a path, and a query string. The scheme part *must* be ``"file:"``.
279+
The query string allows us to pass parameters to SQLite. Some useful URI
280+
tricks include::
281+
282+
# Open a database in read-only mode.
283+
con = sqlite3.connect("file:template.db?mode=ro", uri=True)
284+
285+
# Don't implicitly create a new database file if it does not already exist.
286+
# Will raise sqlite3.OperationalError if unable to open a database file.
287+
con = sqlite3.connect("file:nosuchdb.db?mode=rw", uri=True)
288+
289+
# Create a shared named in-memory database.
290+
con1 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
291+
con2 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
292+
con1.executescript("create table t(t); insert into t values(28);")
293+
row = con2.execute("select * from t").fetchall()
294+
295+
More information about this feature, including a list of recognized
296+
parameters, can be found in the
297+
`SQLite URI documentation <https://www.sqlite.org/uri.html>`_.
284298

285299
.. audit-event:: sqlite3.connect database sqlite3.connect
286300
.. audit-event:: sqlite3.connect/handle connection_handle sqlite3.connect

0 commit comments

Comments
 (0)