The SQLAlchemy in flask is more thorough than the SQLAlchemy package and is simpler in some ways
First import the class library:
View code slices to my Code slice
From flask import flask from flask.ext.sqlalchemy import SQLAlchemy
Then, you need to load the database path
View code slices to my Code slice
Mysqlname= 'Mysql://user:passwd@127.0.0.1/student?charset=utf8'
View code slices to my Code slice
App = Flask (__name__) app.config[' sqlalchemy_database_uri '] = mysqlname db = SQLALCHEMY (APP)
With the first two steps, we've got flask and the database connected.
Now we're going to link the flask with the specific table,
This creates a model.
View code slices to my Code slice
Class User (db. Model): "" stores the number of each type of alarm, in minutes for the statistics :p Aram source:string, Alarm source :p Aram Network_logic_area:string, the alarm belongs to the logos Network area :p Aram Start_time:datetime, alarm occurs "" " __tablename__ = ' Hello ' id = db. Column (db. Integer, Primary_key = True) Source = db. Column (db. String (255)) Network_logic_area = db. Column (db. String (255)) start_time = db. Column (db. DateTime) count = db. Column (db. Integer) def __init__ (self, source, Network_logic_area, Start_time, count): Self.source = Source self . Network_logic_area = Network_logic_area self.start_time = start_time Self.count = Count def alter (self ): Self.count + = 1;
The above code, let Falsk and specific table hello linked together
In this class, we first specify the table and then list the columns in the table, and finally define an initialization function that inserts the data later using the
Now start the specific database operation:
1. Insert
View code slices to my Code slice
p = User (...) Db.session.add (P) db.session.commit ()
A data is constructed from the class user
2. Find
Get the data with the primary key:
Code Example:
User.query.get (1)
To counter-check with an exact parameter:
Code Example:
Peter=user.query.filter_by (Username= ' Peter '). First () #注意: Exact query function query.filter_by (), which is queried by passing parameters Other enhanced query functions are Query.filter (), which are queried by pass-through expressions. Print (peter.id) #如果数据不存在则返回None
Fuzzy query:
Code Example:
User.query.filter (User.email.endswith (' @example. com ')). All () [
,
]
Logic not 1:
Code Example:
Peter=user.query.filter (User.username! = ' Peter '). First () print (peter.id)
Logic not 2:
Code Example:
Fromsqlalchemy Importnot_peter=user.query.filter (Not_ (user.username== ' Peter ')). First () print (peter.id)
Logic with:
Code Example:
Fromsqlalchemy Importand_peter=user.query.filter (And_ (user.username== ' Peter ', User.email.endswith (' @example. com ') )). First () print (peter.id)
Logical OR:
Code Example:
Fromsqlalchemy Importor_peter=user.query.filter (or_ (user.username! = ' Peter ', User.email.endswith (' @example. com ')) ). First () print (peter.id)
Filter_by: This can only be put into a specific condition, can not put a complex calculation,
Filter: You can put some complicated calculations in this.
. First: Take the number one data
. All: Remove all data
There's another way to sort, count things like that
3. Using SQL statements
You can directly use SQL's native statements from the previously constructed DB
View code slices to my Code slice
Insert_table.db.engine.execute (' ... ')
4. Delete
View code slices to my Code slice
me = User (...)
View code slices to my Code slice
Db.session.delete (Me) db.session.commit ()
5. Update data
Code Example:u=user.query.first () u.username= ' guest ' #更新数据和变量赋值那么简单, but must be the object returned by the query. Db.session.commit ()