This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Enhance sqlite3 to avoid implicit creation?
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, erlendaasland, miss-islington, nedbat, zach.ware
Priority: normal Keywords: patch

Created on 2022-01-16 17:42 by nedbat, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30660 merged erlendaasland, 2022-01-18 10:25
PR 30661 closed miss-islington, 2022-01-18 12:37
PR 30662 closed miss-islington, 2022-01-18 12:37
PR 30671 merged erlendaasland, 2022-01-18 20:46
PR 30672 merged erlendaasland, 2022-01-18 20:48
Messages (15)
msg410704 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2022-01-16 17:42
The sqlite3 library implicitly creates the database if the requested file doesn't exist.  I would like to be able to avoid that implicit creation.  (Actually, it would be enough to know whether I had created the database, but the underlying SQLite library doesn't seem to indicate that.)

The C code currently hard-codes the SQLite flag to create the database if it doesn't exist:

    rc = sqlite3_open_v2(database, &db,
                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
                         (uri ? SQLITE_OPEN_URI : 0), NULL);

Could we make this an option, so the Python code could avoid implicit creation?
msg410724 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-01-16 20:32
I agree that would be a useful option.
msg410779 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-17 13:24
FYI, you can already do this using the URI option:

    cx = sqlite3.connect("file:test.db?mode=rw", uri=True)
msg410780 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-17 13:26
See also bpo-24887.
msg410781 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-17 13:31
IMO, the URI "API" is not very pythonic; I have to look up the format every time I'm using it.

OTOH, introducing flags, or keywords, for every option will add a lot of code.
msg410783 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-17 13:40
OTOH, implementing an API similar to apsw (adding a flags keyword) would make it easier for users to switch between that and the stdlib sqlite3.
msg410790 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2022-01-17 14:48
@Erlend: thanks for the URI tip, I missed that as a possibility in the SQLite docs.
msg410793 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-17 14:59
I guess we could do more to promote that trick in the docs. It’s quite useful.
msg410851 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-18 10:28
I created GH-30660 in order to try to enhance the docs. Ned and Eric, would you mind taking a look at the PR?
msg410857 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-01-18 12:37
New changeset bdf2ab1887a2edfb089a3c2a1590cf1e84ea0048 by Erlend Egeberg Aasland in branch 'main':
bpo-46402: Promote SQLite URI tricks in `sqlite3` docs (GH-30660)
https://github.com/python/cpython/commit/bdf2ab1887a2edfb089a3c2a1590cf1e84ea0048
msg410892 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2022-01-18 21:01
BTW, I'm fine with this being closed, since the functionality I wanted is available and documented.
msg410895 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-18 21:09
> BTW, I'm fine with this being closed, since the functionality I wanted is available and documented.

Great. I was considering closing it as soon as the backports have landed (I had to manually fix them bco. make suspicious failures).
msg410902 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-01-18 21:57
New changeset 01e6cbefd3d0f60c942ed711131f5d638dde1227 by Erlend Egeberg Aasland in branch '3.10':
[3.10] bpo-46402: Promote SQLite URI tricks in sqlite3 docs (GH-30660) (GH-30671)
https://github.com/python/cpython/commit/01e6cbefd3d0f60c942ed711131f5d638dde1227
msg410903 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-01-18 21:58
New changeset 0ae22577606f1b52e3b6c2de6c5b307518044605 by Erlend Egeberg Aasland in branch '3.9':
[3.9] bpo-46402: Promote SQLite URI tricks in sqlite3 docs (GH-30660) (#30672)
https://github.com/python/cpython/commit/0ae22577606f1b52e3b6c2de6c5b307518044605
msg410906 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2022-01-18 22:15
Closing this. Thanks, Ned and Eric!
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90560
2022-01-18 22:15:01erlendaaslandsetstatus: open -> closed
resolution: fixed
messages: + msg410906

stage: patch review -> resolved
2022-01-18 21:58:50eric.smithsetmessages: + msg410903
2022-01-18 21:57:47eric.smithsetmessages: + msg410902
2022-01-18 21:09:55erlendaaslandsetmessages: + msg410895
2022-01-18 21:01:38nedbatsetmessages: + msg410892
2022-01-18 20:48:23erlendaaslandsetpull_requests: + pull_request28872
2022-01-18 20:46:00erlendaaslandsetpull_requests: + pull_request28871
2022-01-18 12:37:30eric.smithsetmessages: + msg410857
2022-01-18 12:37:16miss-islingtonsetpull_requests: + pull_request28863
2022-01-18 12:37:12miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request28862
2022-01-18 10:28:37erlendaaslandsetmessages: + msg410851
2022-01-18 10:25:29erlendaaslandsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request28861
2022-01-17 14:59:25erlendaaslandsetmessages: + msg410793
2022-01-17 14:48:15nedbatsetmessages: + msg410790
2022-01-17 13:40:01erlendaaslandsetmessages: + msg410783
2022-01-17 13:31:13erlendaaslandsetmessages: + msg410781
2022-01-17 13:26:17erlendaaslandsetmessages: + msg410780
2022-01-17 13:24:54erlendaaslandsetmessages: + msg410779
2022-01-16 20:32:57eric.smithsetnosy: + eric.smith
messages: + msg410724
2022-01-16 20:19:30zach.waresetnosy: + zach.ware, erlendaasland
stage: needs patch
type: enhancement

versions: + Python 3.11
2022-01-16 17:42:31nedbatcreate