Is there a way to yield an error when opening a nonexistent dbfile?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a way to yield an error when opening a nonexistent dbfile?

Peng Yu
Hi,

By default the command sqlite3 will just open a dbfile if it does not exist.

Suppose that I just want to perform read-only operations in a sqlite3
session, I will not need to create a non-exsitent file. Rather, I want
the sqlite3 to fail when the dbfile does not exist. Is there a way to
achieve this?

Thanks.

--
Regards,
Peng
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to yield an error when opening a nonexistent dbfile?

Kees Nuyt
On Fri, 31 Jan 2020 09:02:10 -0600, Peng wrote:

> Hi,
>
> By default the command sqlite3 will just open a dbfile if it does not exist.
>
> Suppose that I just want to perform read-only operations in a sqlite3
> session, I will not need to create a non-exsitent file. Rather, I want
> the sqlite3 to fail when the dbfile does not exist. Is there a way to
> achieve this?

$ sqlite3 -readonly testx.db
SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> .tables
Error: unable to open database "testx.db": unable to open database file

--
Regards,
Kees Nuyt
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to yield an error when opening a nonexistent dbfile?

Peng Yu
What is the recommended way to do so in python?

I see the following two ways.

```
db = sqlite3.connect('file:/path/to/database?mode=ro', uri=True)

fd = os.open(filename, os.O_RDONLY)
c = sqlite3.connect('/dev/fd/%d' % fd)
os.close(fd)
```

https://stackoverflow.com/questions/10205744/opening-sqlite3-database-from-python-in-read-only-mode

But can the first have problems when the filename is actually
something like 'file:database?mode=ro'?

For my applications, I only unix like platform. So the second way may be better?

Any other better solutiosn with the python sqlite3 library?

The stackoverflow link also mentions APSW. It seems that it is quite
updated. Is this a good replacement for the python sqlite3 module in
general?

Thanks.

> $ sqlite3 -readonly testx.db
> SQLite version 3.31.1 2020-01-27 19:55:54
> Enter ".help" for usage hints.
> sqlite> .tables
> Error: unable to open database "testx.db": unable to open database file

--
Regards,
Peng
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to yield an error when opening a nonexistent dbfile?

Keith Medcalf

You could use the second method (opening the file by handle) if you do not want your code to be portable.

Yes, APSW is far superior to sqlite3.  It does not have any "magic" and wraps SQLite3 into Python so that it works like SQLite3 works, so the interface works as documented for the equivalent "C" code on the sqlite.org website (or on the APSW documentation website at https://rogerbinns.github.io/apsw/ and the documentation is MUCH more complete than the paltry stuff available for the pysqlite2/sqlite3 wrapper).  

The author of APSW also happens to be on this mailing list, and APSW is generally updated shortly after a new version of SQLite3 is released (or you can compile it yourself if you wish).  If you find a bug it is also highly likely that it will be fixed (unlike the pysqlite2/sqlite3 wrapper in Python which has not been updated in years and is 20-odd versions behind its parent project code).  APSW can be found here:  https://github.com/rogerbinns/apsw

With APSW you simply pass the open flags you want to use on the Connection constructor just like you would do in C.

About the only things that APSW does not have that the sqlite3 does is the Row object and the data adapters and converters.

--
The fact that there's a Highway to Hell but only a Stairway to Heaven says a lot about anticipated traffic volume.

>-----Original Message-----
>From: sqlite-users <[hidden email]> On
>Behalf Of Peng Yu
>Sent: Friday, 31 January, 2020 17:49
>To: SQLite mailing list <[hidden email]>
>Subject: Re: [sqlite] Is there a way to yield an error when opening a
>nonexistent dbfile?
>
>What is the recommended way to do so in python?
>
>I see the following two ways.
>
>```
>db = sqlite3.connect('file:/path/to/database?mode=ro', uri=True)
>
>fd = os.open(filename, os.O_RDONLY)
>c = sqlite3.connect('/dev/fd/%d' % fd)
>os.close(fd)
>```
>
>https://stackoverflow.com/questions/10205744/opening-sqlite3-database-
>from-python-in-read-only-mode
>
>But can the first have problems when the filename is actually
>something like 'file:database?mode=ro'?
>
>For my applications, I only unix like platform. So the second way may be
>better?
>
>Any other better solutiosn with the python sqlite3 library?
>
>The stackoverflow link also mentions APSW. It seems that it is quite
>updated. Is this a good replacement for the python sqlite3 module in
>general?
>
>Thanks.
>
>> $ sqlite3 -readonly testx.db
>> SQLite version 3.31.1 2020-01-27 19:55:54
>> Enter ".help" for usage hints.
>> sqlite> .tables
>> Error: unable to open database "testx.db": unable to open database file
>
>--
>Regards,
>Peng
>_______________________________________________
>sqlite-users mailing list
>[hidden email]
>http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users



_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users