Connecting to an online database through python sqlite3

Viewed 1826

I have a question about sqlite3. If I were to host a database online, how would I access it through python's sqlite3 module?

E.g. Assume I had a database hosted at "www.example.com/database.db". Would it be as simple as just forming a connection with sqlite3.connect ("www.example.com/database.db") or is there more I need to add so that the string is interpreted as a url and not a filename?

2 Answers

SQLite3 is embedded-only database so it does not have network connection capabilities. You will need to somehow mount the remote filesystem.

With that being said, SQLite3 is not meant for this. Use PostgreSQL or MySQL (or anything else) for such purposes.

You can download the database and then read it but you can't connect to a SQLite Database. First create the downloader:

def download_file(path):
r = requests.get(path)
filename = path.split("/")[-1]
fullname = str(os.getcwd())+"/"+filename
 
with open(fullname, 'wb') as f:
    f.write(r.content)
    print ("DB downloaded")

Then download the database:

download_file("www.example.com/database.db")

and finally open, read, etc...
https://www.tutorialspoint.com/sqlite/sqlite_python.htm

to delete the database: (first import os module)

os.system("rm /path/to/database.db")
Related