Skip to content

Commit 4c14a65

Browse files
Erlend Egeberg Aaslandmiss-islington
authored andcommitted
bpo-46402: Promote SQLite URI tricks in sqlite3 docs (pythonGH-30660)
Provide some examples of URI parameters in sqlite connect(). Co-authored-by: Ned Batchelder <[email protected]> (cherry picked from commit bdf2ab1) Co-authored-by: Erlend Egeberg Aasland <[email protected]>
1 parent 1d6530d commit 4c14a65

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
@@ -254,14 +254,28 @@ Module functions and constants
254254
for the connection, you can set the *cached_statements* parameter. The currently
255255
implemented default is to cache 100 statements.
256256

257-
If *uri* is true, *database* is interpreted as a URI. This allows you
258-
to specify options. For example, to open a database in read-only mode
259-
you can use::
260-
261-
db = sqlite3.connect('file:path/to/database?mode=ro', uri=True)
262-
263-
More information about this feature, including a list of recognized options, can
264-
be found in the `SQLite URI documentation <https://www.sqlite.org/uri.html>`_.
257+
If *uri* is :const:`True`, *database* is interpreted as a
258+
:abbr:`URI (Uniform Resource Identifier)` with a file path and an optional
259+
query string. The scheme part *must* be ``"file:"``. The path can be a
260+
relative or absolute file path. The query string allows us to pass
261+
parameters to SQLite. Some useful URI tricks include::
262+
263+
# Open a database in read-only mode.
264+
con = sqlite3.connect("file:template.db?mode=ro", uri=True)
265+
266+
# Don't implicitly create a new database file if it does not already exist.
267+
# Will raise sqlite3.OperationalError if unable to open a database file.
268+
con = sqlite3.connect("file:nosuchdb.db?mode=rw", uri=True)
269+
270+
# Create a shared named in-memory database.
271+
con1 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
272+
con2 = sqlite3.connect("file:mem1?mode=memory&cache=shared", uri=True)
273+
con1.executescript("create table t(t); insert into t values(28);")
274+
rows = con2.execute("select * from t").fetchall()
275+
276+
More information about this feature, including a list of recognized
277+
parameters, can be found in the
278+
`SQLite URI documentation <https://www.sqlite.org/uri.html>`_.
265279

266280
.. audit-event:: sqlite3.connect database sqlite3.connect
267281

0 commit comments

Comments
 (0)