Skip to content

Commit bdf2ab1

Browse files
Erlend Egeberg Aaslandnedbat
andauthored
bpo-46402: Promote SQLite URI tricks in sqlite3 docs (GH-30660)
Provide some examples of URI parameters in sqlite connect(). Co-authored-by: Ned Batchelder <[email protected]>
1 parent 8c2fd09 commit bdf2ab1

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)` with a file path and an optional
278+
query string. The scheme part *must* be ``"file:"``. The path can be a
279+
relative or absolute file path. The query string allows us to pass
280+
parameters to SQLite. Some useful URI 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+
rows = 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)