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

Skip to content

Commit 01e6cbe

Browse files
author
Erlend Egeberg Aasland
authored
[3.10] bpo-46402: Promote SQLite URI tricks in sqlite3 docs (GH-30660) (GH-30671)
* 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 4449a16 commit 01e6cbe

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

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

267281
.. audit-event:: sqlite3.connect database sqlite3.connect
268282
.. audit-event:: sqlite3.connect/handle connection_handle sqlite3.connect

Doc/tools/susp-ignored.csv

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

0 commit comments

Comments
 (0)
0