Forty-six: Use Flask-SQLAlchemy database of

 

A: connect to the database
1, the installation: the install Flask-SQLAlchemy PIP
2, the database information is updated to the app.config [ 'SQLALCHEMY_DATABASE_URI']
. 3, using a class definition flask_sqlalchemy.SQLAlchemy object and pass into the app: db = SQLAlchemy ( app)

II: Create ORM model
1, and use SQLAlchemy or win, model definition, eliminating the need to create a base class delaretive_base, but using as a base class to db.Model
2, model class, Column, String, Integer, relationship ??? do not need to import directly use the property under the name db can be
3, in the definition of the model, they can not write __tablename __, flask-sqlalchemy will use the name of the current model to lowercase table names as the default, if it is camelCase (AxxBxx) will be converted into
an underscore split (axx_bxx)

III: The ORM model mapped to the database
. 1, db.drop_all ()
2, db.create_all ()

4: Use session
using the session does not need to create a sessionmaker, eh directly with db.session

Five: query data
, if only to find data on a model by the same method to find the model .query, query just like SQLAlchemy query method of the same

 

Guess you like

Origin www.cnblogs.com/zhongyehai/p/11830929.html