Forums

Downloading static db file?

I have an sqlite database which gets read as well as updated through my webapp.. I'm trying to implement a backup system where the db file can be downloaded, which I'm using flask's send_file method. Yet the downloaded file does not have updated data, and the hosted files as well! Is there anyway to do this?

We need more detail abut your code and the state of your files. You can send it to support@pythonanywhere.com if you do not want to post it in public.

[formatted by admin]

@app.route("/backup", methods=['POST'])
def backup():
    today = date.today()
    name = "store_" + today.strftime("%d%b%Y") + ".db"
    return send_file("store.db", as_attachment=True, download_name=name)

Here's the relevant code in question. "store.db" is a file in the same directory as flask_app.py. The db file I end up with is the version I uploaded, without any of the injections or updates done through the web app.

What's the working directory set for your web app? If it's your HOME directory, then your code will look for the store.db file in /home/inhamoninven/. Have a look at your web app error logs and check if you see FileNotFoundError errors.

That is correct, it is in the right place. It does find the file, no errors, the file just seems to be the offline version. Yet the web app retains all the updates to the database!

Just to clarify what @pafk was saying: your website is configured with a working directory of /home/inhamoninven/ on the "Web" page, and so send_file will be trying to send the file /home/inhamoninven/store.db. If you want to send the file from the same location as your flask_app.py, which I think (from your website settings) is in /home/inhamoninven/mysite, then the quickest way would be to use an absolute path, like this:

return send_file("/home/inhamoninven/mysite/store.db", as_attachment=True, download_name=name)

I have done that before, and that gives me a file not found error, which led to my current iteration of the code.

I have just tried it again, and the error logs state:

FileNotFoundError: [Errno 2] No such file or directory: '/home/inhamoninven/mysite/home/inhamoninven/mysite/store.db'

I've finally understood the problem, there was an issue with my sqlite commit sequences! So it has nothing to do with the download. Thank you all for the help! My mistake.

Glad you figured that out!