Why do I have to change the uri in Flask for SQLAlchemy?

15,855

Solution 1

I'm a little unclear what you're asking, where you're setting this DATABASE value and where the flaskr.json value is coming from. But I'll give it a shot, and hopefully it'll be of some use.

So, obviously, you need to tell your app how to connect to your database. You generally set this up in the app configuration like so:

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'

For SQLlite, you just need a file path (as above) For MySQL, I have our app set up like so:

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://username:[email protected]/database'

You can read way more than you ever wanted to know about this in the SQLAlchemy docs:

http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls

Also, have you looked at the Flask-SQLAlchemy plugin? It gives you some nice tools to work with this.

http://flask-sqlalchemy.pocoo.org/2.3/quickstart/#a-minimal-application

Solution 2

For windows use this

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///E:\\account-verification-flask\\src\\example.db'
# sqlite:///absolute path

For more information. visit here

Share:
15,855
matt hoover
Author by

matt hoover

N/A

Updated on June 04, 2022

Comments

  • matt hoover
    matt hoover almost 2 years

    I am trying to develop a web application in Flask, and I have noticed that if I want to use SQLite3 as my database, then I have to input

    DATABASE = 'flaskr.json'
    

    However, if I want to use SQLAlchemy for my database implementation, I have to use something like

    DATABASE = 'sqlite:////Users/jake/repos/flaskralchemy/flaskr.db
    

    What is the significance of this change? Also, in all the examples that I have seen, no one creates an actual database on their server. What is going on?

    Thanks in advance.