-->

Connect to a remote sqlite3 database with Python

2019-02-25 21:11发布

问题:

I am able to create a connection to a local sqlite3 database ( Using Mac OS X 10.5 and Python 2.5.1 ) with this:

conn = sqlite3.connect('/db/MyDb')

How can I connect to this database if it is located on a server ( for example on a server running Ubuntu 8.04 with an IP address of 10.7.1.71 ) , and is not stored locally?

e.g. this does not seem to work:

conn = sqlite3.connect('10.7.1.71./db/MyDb')

回答1:

SQLite is embedded-only. You'll need to mount the remote filesystem before you can access it. And don't try to have more than one machine accessing the SQLite database at a time; SQLite is not built for that. Use something like PostgreSQL instead if you need that.



回答2:

The sqlite FAQ has an answer relevant to your question. It points out that although multi-machine network access is theoretically possible (using a remote filesystem) it likely won't be reliable unless the filesystem properly supports locks.

If you're accessing it from only one machine and process at a time, however, it should work acceptably, as that page notes (and dependent on the remote filesystem you're using).