Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify how sqlite3 maps parameters onto placeholders #100668

Closed
erlend-aasland opened this issue Jan 1, 2023 · 2 comments · Fixed by #100960
Closed

Clarify how sqlite3 maps parameters onto placeholders #100668

erlend-aasland opened this issue Jan 1, 2023 · 2 comments · Fixed by #100960
Assignees
Labels
docs Documentation in the Doc dir topic-sqlite3

Comments

@erlend-aasland
Copy link
Contributor

erlend-aasland commented Jan 1, 2023

agree; you might want to clarify though that the use of "qmark" or "named" is detected automatically on a per-statement basis (provided my understanding of that is correct).

Yes, we should definitely clarify how parameters are interpreted and mapped to the placeholders.

sqlite3 does not check if you use the "qmark" or "named" style (or any other style FWIW1); it only looks at the type of the params supplied:

  1. If a dict or dict subclass is supplied, the named style is assumed and you'll get an error if a named parameter is not provided by the supplied dict.

  2. If an exact tuple, an exact list, or a sequence (that is not a dict or dict subclass) is supplied, the qmark style2 is assumed. This means that sqlite3 iterates over the params and blindly assigns placeholder 13 the first item in the supplied sequence, and so on. This also happens if you use named placeholders and supply, for example, a list. Try it and be surprised. Now, that bug may be too old to be fixed; there's bound to be some code out there that depends on this exact bug. We might be able to introduce a warning and then change the behaviour after a few release cycles, but such a breaking change/bugfix will need a broader discussion.

Originally posted by @erlend-aasland in #100630 (comment)

Linked PRs

Footnotes

  1. try for example cx.execute("select ?2, ?1", ['first', 'second']); the SQLite numeric style, which is not PEP-249-compatible, is accepted and correctly applied

  2. called nameless in SQLite speak

  3. SQLite placeholders use one-based indices

@erlend-aasland
Copy link
Contributor Author

See also #99953

@erlend-aasland erlend-aasland moved this from TODO: Docs to In Progress in sqlite3 issues Jan 11, 2023
@erlend-aasland
Copy link
Contributor Author

We should not put too much details into the existing "placeholders how-to"; perhaps we could add a short explanation for how this stuff actually works.

@github-project-automation github-project-automation bot moved this from In Progress to Done in sqlite3 issues Jan 14, 2023
erlend-aasland added a commit that referenced this issue Jan 14, 2023
…0960)

Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 14, 2023
pythonGH-100960)

(cherry picked from commit 206f05a)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 14, 2023
pythonGH-100960)

(cherry picked from commit 206f05a)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
miss-islington added a commit that referenced this issue Jan 14, 2023
…100960)

(cherry picked from commit 206f05a)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
miss-islington added a commit that referenced this issue Jan 14, 2023
…100960)

(cherry picked from commit 206f05a)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir topic-sqlite3
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant