The SQLAlchemy in flask is more thorough than the SQLAlchemy package, and is simpler in some ways
First Import class Library:
View a code slice from my Code chip
<span style= "FONT-SIZE:18PX;" >from flask Import Flask from
flask.ext.sqlalchemy import sqlalchemy</span>
Then, you need to load the database path
View a code slice from my Code chip
<span style= "FONT-SIZE:18PX;" >mysqlname= ' <span style= ' Color:rgb (230, 219, 116); font-family: ' Source Code Pro '; font-size:13pt; Background-color:rgb (39, 40, 34); >mysql://user:passwd@127.0.0.1/student?charset=utf8</span> ' </span>
View a code slice from my Code chip
<span style= "FONT-SIZE:18PX;" >app = Flask (__name__)
app.config[' sqlalchemy_database_uri '] = mysqlname
db = SQLALCHEMY (APP) </span >
With the first two steps, we've got flask and the database linked together.
Below we will link the flask with the concrete table,
This creates a model
View a code slice from my Code chip
<span style= "FONT-SIZE:18PX;" >class User (db. Model): ""
store the number of each type of alarm, in minutes for 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 occurrence Time "" "
__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;</span>
The above code, let Falsk and specific table Hello together
In this class, we first specify the table, then list the columns in the table, and finally define an initialization function that inserts the data in the following
Now start the specific database operation:
1, insert
View a code slice from my Code chip
<span style= "FONT-SIZE:18PX;" > p = User (...)
Db.session.add (P)
db.session.commit () </span>
Constructs a piece of data from the class user
2, find
To get data with a primary key:
Code Example:
User.query.get (1)
<user
u ' admin ' >
Reverse-check with an exact parameter:
Code Example:
Peter
=
User.query.filter_by (username= ' Peter ').
#注意: Exact query function query.filter_by (), is queried by passing parameters The other enhanced query functions are Query.filter (), which are queried by passing an expression.
print (peter.id)
#如果数据不存在则返回None
Fuzzy query:
Code Example:
User.query.filter (User.email.endswith (' @example. com ')). All ()
[<user
u ' admin ',
<user U ' guest ';
Logical non 1:
Code Example:
Peter
=
User.query.filter (user.username
!=
' Peter '). (a)
print (peter.id)
Logical Non 2:
Code Example:
From
sqlalchemy import
not_
peter
=
User.query.filter (not_ (user.username== ' Peter ')). A (
peter.id) print
Logic and:
Code Example:
from SQLAlchemy import
and_
peter
=
User.query.filter (and_ user.username== ' Peter ',
User.email.endswith (' @example. com ')). A (
peter.id) print
Logical OR:
Code Example:
from SQLAlchemy import
or_
peter
=
User.query.filter (or_ user.username
!=
' Peter ',
User.email.endswith (' @example. com ')). A (
peter.id) print
Filter_by: This inside can only put the specific conditions, can not put a complex calculation,
Filter: This one can put some complex calculations
. First: Take the number one data
. All: Remove all data
There's another way to do things like sort, count, and so on.
3. Use SQL statements
You can use SQL Native statements directly from the previously constructed DB
View a code slice from my Code chip
<span style= "FONT-SIZE:18PX;" >insert_table.db.engine.execute (' ..... ') </span>
4, delete
View a code slice from my Code chip
<span style= "FONT-SIZE:18PX;" >me = User (...) </span>
View a code slice from my Code chip
<span style= "FONT-SIZE:18PX;" >db.session.delete (Me)
db.session.commit () </span>
5, update the data
Code example:
u
=
User.query.first ()
u.username
=
' guest '
#更新数据和变量赋值那么简单, However, it must be an object returned through a query.
Db.session.commit ()