Forums

How to upload existing SQLite3 database?

I have a Flask app that interacts with a SQLite3 database. I wrote a Python script that sets up the tables, columns, etc. in the database automatically. I uploaded this file to my app folder (/home/Ridecloud/mysite/ridelog_db_setup.py), and it automatically created the database in my app's root folder (/home/Ridecloud/ridelog_db.db).

Is there a way to just point SQL Alchemy to this file? If not, how can I tell PythonAnywhere to create a database as specified in my database setup file?

What have you tried so far? If you give sqlalchemy the full path to the database file, it should work fine?

Just make sure it's the full path, including the /home/Ridecloud...

In my ridelog_db_setup.py file:

engine = create_engine('sqlite:////home/Ridecloud/mysite/ridelog_db.db')

In my flask_app.py file:

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////home/Ridecloud/mysite/ridelog_db.db'
engine = create_engine('sqlite:////home/Ridecloud/mysite/ridelog_db.db')

Whenever I try to do something on my website that interacts with the database, I get a 500 internal server error. I also tried to do it with MySQL:

In ridelog_db_setup.py:

engine = create_engine('mysql://Ridecloud:mypassword@mysql.server/Ridecloud$default', pool_recycle = 280)

in flask_app.py:

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://Ridecloud:mypassword@mysql.server/Ridecloud$default'
engine = create_engine('mysql://Ridecloud:mypassword@mysql.server/Ridecloud$default', pool_recycle = 280)

What does it say in your error log?

Hmm...

RuntimeError: the session is unavailable because no secret key was set.  Set the secret_key on the application to something unique and secret.

I set a secret key in my flask_app.py, but I guess that part of the code isn't being run since it's being handled by PythonAnywhere's server(?)

if __name__ == '__main__':
app.secret_key = 'mysecretkey'

EDIT: IT WORKS!! I set the app.secret_key outside of the function shown above. Thanks so much for the help, Harry! While I've got your attention, what was I doing wrong in my MySQL code?

Glad it works! I couldn't see anything obviously wrong with the mysql code?

I know it was a long time ago but I have the same problem... could you tell me what code you used in the end? :)

Just a quick note to join this to @Rev1337's other thread.