Creating a SQLAlchemy DB instance
SQLAlchemy is a Python SQL toolkit and provides an ORM that gives the flexibility and power of SQL with the feel of Python's object-oriented nature.
Getting ready
Flask-SQLAlchemy is the extension that provides the SQLAlchemy interface for Flask.
This extension can be simply installed using pip
as follows:
$ pip install flask-sqlalchemy
The first thing to keep in mind with Flask-SQLAlchemy is the application config parameter that tells SQLAlchemy about the location of the database to be used:
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ('DATABASE_URI')
This SQLALCHEMY_DATABASE_URI
is a combination of the database protocol, any authentication needed, and also the name of the database. In the case of SQLite, this would look something like the following:
sqlite:////tmp/test.db
In the case of PostgreSQL, it would look like the following:
postgresql://yourusername:yourpassword@localhost/yournewdb.
This extension then provides a class named...