Installation method
1), Apt-ge installation
The code is as follows:
sudo apt-get install Flask-sqlalchemy
2), download the installation package for installation
The code is as follows:
# can be used directly in the PY after installation
Import Flask
From Flask.ext.sqlalchemy import SQLAlchemy
App = Flask. Flask (__name__)
#-Settings is configured with SQLAlchemy database address
# SQLite ex: "Sqlite:///dbname.db"
App.config.from_object ("Settings")
db = SQLAlchemy (APP)
Db.init_app (APP)
#-Create_all () will create all inherited db when called. Model Template
# Model ex: see Class Admininfo
Db.create_all ()
Class Admininfo (db. Model):
ID = db. Column (db. Integer, Primary_key = True)
Name = db. Column (db. String (16))
Password = db. Column (db. String (32))
Kidname = db. Column (db. String (16))
Diy_show = db. Column (db. Text)
def __init__ (self, name, password, kidname, diy_show):
Self.name = Name
Self.password = password
Self.kidname = Kidname
Self.diy_show = Diy_show
def __repr__ (self):
Return " "% (self.name, ' * ' *len (Self.password))
This means that you can use SQLAlchemy in the render template.
The code is as follows:
# operate on the Admininfo
ai = Admininfo ("gaoyiping", "gaoyiping", U "I call Gao Yiping", u "Hello everyone, my name is Gao Yiping, what's your name?" Let's make a friend. ")
# so we've already instantiated a SQL Data
# Insert the DB
Db.session.add (AI)
# for DB commit
Db.session.commit ()
# If you make a query
AdminInfo.query.all ()
# >>> [ ,]
AdminInfo.query.get (1) # Query the first record you just inserted
# >>>
AdminInfo.query.filter_by (name = "Gaoyiping")
# >>>