This issue tracker will soon become read-only and move to GitHub.
For a smoother transition, remember to log in and link your GitHub username to your profile.
For more information, see this post about the migration.

classification
Title: sqlite3.connect() should check if the sqlite file exists and throw a FileNotFoundError if it doesn't
Type: Stage:
Components: Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: erlendaasland, iboates, lemburg
Priority: normal Keywords:

Created on 2021-09-21 10:10 by iboates, last changed 2021-09-22 07:36 by lemburg.

Messages (3)
msg402309 - (view) Author: Isaac Boates (iboates) Date: 2021-09-21 10:10
I was just using the sqlite3 package and was very confused when trying to open an sqlite database from a relative path, because the only error provided was:

  File "/path/to/filepy", line 50, in __init__
    self.connection = sqlite3.connect(path)
sqlite3.OperationalError: unable to open database file

It turns out I was just executing Python from the wrong location and therefore my relative path was broken. Not a big problem. But it was confusing because it only throws this generic OperationalError.

Could it instead throw a FileNotFoundError if the db simply doesn't exist at the specified path?
msg402363 - (view) Author: Erlend E. Aasland (erlendaasland) * (Python triager) Date: 2021-09-21 20:43
> Could it instead throw a FileNotFoundError if the db simply doesn't exist at the specified path?

We pass the database path pretty much straight to sqlite3_open_v2(), via the os.PathLike interface, so any errors we get come straight from SQLite (apart from os.PathLike errors). We translate SQLite error codes to the corresponding DP-API exception, as defined per PEP 249.

Raising FileNotFoundError would mean adding support for validating the path prior to calling sqlite3_open_v2() _and_ maintaining backwards compatibility (in-memory database, creating new database file if the path points to a valid directory, etc.). This will complicate the code unnecessary, IMO. I suggest to keep the current behaviour.

See also the slightly related bpo-24139.
msg402409 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2021-09-22 07:36
Such a change would be backwards incompatible and no longer in line with PEP 249.

I also don't understand what you regard as confusing about the message "unable to open database file". The message could be extended to include the path, but apart from that, it's as clear as it can get :-)
History
Date User Action Args
2021-09-22 07:36:10lemburgsetstatus: pending -> open
nosy: + lemburg
messages: + msg402409

2021-09-21 20:44:02erlendaaslandsetstatus: open -> pending
2021-09-21 20:43:52erlendaaslandsetmessages: + msg402363
2021-09-21 14:30:49erlendaaslandsetnosy: + erlendaasland
2021-09-21 10:10:04iboatescreate