8000 [3.9] bpo-46402: Promote SQLite URI tricks in sqlite3 docs (GH-30660)… · python/cpython@0ae2257 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ae2257

Browse files
author
Erlend Egeberg Aasland
authored
[3.9] bpo-46402: Promote SQLite URI tricks in sqlite3 docs (GH-30660) (#30672)
* 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 <ned@nedbatchelder.com> (cherry picked from commit bdf2ab1) Co-authored-by: Erlend Egeberg Aasland <erlend.aasland@innova.no> * Update suspicious rules
1 parent 8527f7a commit 0ae2257

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
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

Doc/tools/susp-ignored.csv

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ library/socket,,:can,"return (can_id, can_dlc, data[:can_dlc])"
213213
library/socket,,:len,fds.frombytes(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
214214
library/sqlite3,,:year,"cur.execute(""select * from lang where first_appeared=:year"", {""year"": 1972})"
215215
library/sqlite3,,:memory,
216-
library/sqlite3,,:path,"db = sqlite3.connect('file:path/to/database?mode=ro', uri=True)"
216+
library/sqlite3,,:template,"con = sqlite3.connect(""file:template.db?mode=ro"", uri=True)"
217+
library/sqlite3,,:nosuchdb,"con = sqlite3.connect(""file:nosuchdb.db?mode=rw"", uri=True)"
218+
library/sqlite3,,:mem1,"con1 = sqlite3.connect(""file:mem1?mode=memory&cache=shared"", uri=True)"
219+
library/sqlite3,,:mem1,"con2 = sqlite3.connect(""file:mem1?mode=memory&cache=shared"", uri=True)"
217220
library/ssl,,:My,"Organizational Unit Name (eg, section) []:My Group"
218221
library/ssl,,:My,"Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc."
219222
library/ssl,,:myserver,"Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com"

0 commit comments

Comments
 (0)
0