Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

amazon web services - SQLite Flask - Can't open database file in EC2 when running db.create_all() command

I just deployed a flask app to EC2 AWS, and when trying to initialise a sqlite db by using the db.create_all(bind=['layout']) method, an error was shown

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file

Here is my code

application.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://{}:{}@{}/{}".format(os.getenv('DB_USER'), os.getenv('DB_PASSWORD'), os.getenv('DB_SERVER'), os.getenv('DB_NAME'))
application.config['SQLALCHEMY_BINDS'] = {
    'dashboard': "postgresql://{}:{}@{}/{}".format(os.getenv('DB_USER'), os.getenv('DB_PASSWORD'), os.getenv('DB_SERVER'), os.getenv('DB_NAME')),
    'layout': "sqlite:///layout.db"
}

I ran db.create_all(bind=['layout']) in the python shell within the ec2 terminal, and I am not sure why I ran into the error, as it worked perfectly fine on my localhost.

The postgresql database works fine, and can be accessed properly, but the sqlite database, which is the one im trying to initialise with db.create_all(bind=['layout']) leads to the error shown above, and I am not sure why it isnt creating a 'layout.db' file within the current directory.

I am new to deployment, and am not sure if I did anything wrong, do let me know if I configured anything wrongly, thank you!

question from:https://stackoverflow.com/questions/65903072/sqlite-flask-cant-open-database-file-in-ec2-when-running-db-create-all-comm

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"sqlite:///layout.db" may end up pointing to a place where your applications doesn't have permission to write. Instead try something like

basedir = os.path.abspath(os.path.dirname(__file__))
...
layout': 'sqlite:///' + os.path.join(basedir, 'layout.db')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...