Flask-SQLALchemy 是一个给你的应用添加 SQLALchemy 支持的 Flask 扩展。SQLALchemy 是Python语言的SQL工具包及对象关系映射(ORM)工具,使用MIT许可证发行,提供能兼容众多数据库(如 SQLite、MySQL、Postgres、Oracle、MS-SQL、SQLServer 和 Firebird)的企业级持久性模型。
一、为你的Flask应用加载Flask-SqlAlchemy扩展
Code example:
1
2
3
4
5
6
|
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config[ 'SQLALCHEMY_DATABASE_URI' ] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
|
SQLALCHEMY_DATABASE_URI格式实例:
postgresql://scott:tiger@localhost/mydatabase
mysql://scott:tiger@localhost/mydatabase
oracle://scott:tiger@127.0.0.1:1521/sidname
sqlite:////absolute/path/to/foo.db #注意:有3个斜杠+路径
二、建立数据库模型和初始化数据库
建立数据库模型:
Code example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import hashlib
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key = True )
username = db.Column(db.String( 80 ), unique = True )
email = db.Column(db.String( 320 ), unique = True )
password = db.Column(db.String( 32 ), nullable = False )
def __init__( self , username, email, password):
self .username = username
self .email = email
self .password = hashlib.md5(password)
def __repr__( self ):
return "<User '{:s}'>" . format ( self .username)
|
初始化数据库也特别简单,只需要调用 db.create_all() 函数就可以了。
Code example:
1
2
|
if __name__ = = '__main__' :
db.create_all()
|
三、插入数据
Code example:
1
2
3
|
u = User(username = 'peter' , email = 'test@example.com' , password = '123456' )
db.session.add(u)
db.session.commit()
|
四、查询数据
用主键获取数据:
Code example:
1
2
|
User.query.get( 1 )
<User u 'admin' >
|
通过一个精确参数进行反查:
Code example:
1
2
|
peter = User.query.filter_by(username = 'peter' ).first()
print (peter. id )
|
模糊查询:
Code example:
1
2
|
User.query. filter (User.email.endswith( '@example.com' )). all ()
[<User u 'admin' >, <User u 'guest' >]
|
逻辑非1:
Code example:
1
2
|
peter = User.query. filter (User.username ! = 'peter' ).first()
print (peter. id )
|
逻辑非2:
Code example:
1
2
3
|
from sqlalchemy import not_
peter = User.query. filter (not_(User.username = = 'peter' )).first()
print (peter. id )
|
逻辑与:
Code example:
1
2
3
|
from sqlalchemy import and_
peter = User.query. filter (and_(User.username = = 'peter' , User.email.endswith( '@example.com' ))).first()
print (peter. id )
|
逻辑或:
Code example:
1
2
3
|
from sqlalchemy import or_
peter = User.query. filter (or_(User.username ! = 'peter' , User.email.endswith( '@example.com' ))).first()
print (peter. id )
|
六、查询数据加工
排序和限制函数可以跟在query或filter后面。
排序:
Code example:
1
2
|
User.query.order_by(User.username)
[<User u 'admin' >, <User u 'guest' >, <User u 'peter' >]
|
限制返回的数目:
Code example:
1
2
|
User.query.limit( 1 ). all ()
[<User u 'admin' >]
|
六、查询数据返回
返回查询到的第一个对象:
Code example:
1
2
|
r = User.query.first()
print (r)
|
返回所有查询到的对象:
Code example:
1
2
|
r = User.query. all ()
print (r)
|
七、删除数据
Code example:
1
2
3
|
u = User.query.first()
db.session.delete(u)
db.session.commit()
|
八、更新数据
Code example:
1
2
3
|
u = User.query.first()
u.username = 'guest'
db.session.commit()
|