Flask的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:123456@localhost:3306/pymysqls"
# 如果设置成 True (默认情况),Flask-SQLAlchemy 将会追踪对象的修改并且发送信号。
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True

# 绑定app至SQLAlchemy
db = SQLAlchemy(app)

#会员模型
class User(db.Model):
# __tablename__ = 'user'
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(100),unique=True)
pwd= db.Column(db.String(100))
email= db.Column(db.String(100))
phone= db.Column(db.String(11))
info= db.Column(db.Text)
face = db.Column(db.String(255))
addtime = db.Column(db.DateTime,default=datetime.now)

class Stu(db.Model):
id = db.Column(db.Integer,primary_key=True)
sname = db.Column(db.String(10))
email = db.Column(db.String(50))
age = db.Column(db.Integer)

if __name__ == "__main__":
db.create_all()
  • 模型的操作,在项目下的models.py文件输入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# # 查询数据
# res = User.query.all()
# print(res)
# print(res[0].name)
# print(res[0].email)

# 添加
# s = Stu()
# s.sname = '张三'
# s.email = 'zs@qq.com'
# s.age = 22
# db.session.add(s)
# db.session.commit()
# 添加
# data = {'sname':'lisi','email':'ls@qq.com','age':20}
# s = Stu(**data)
# db.session.add(s)
# db.session.commit()

# 删除
# # 先获取对象
# ob = Stu.query.get(2)
# # print(ob.sname)
# db.session.delete(ob)
# db.session.commit()

# 修改
# ob = Stu.query.get(1)
# ob.sname = 'zhangsansa'
# db.session.add(ob)
# db.session.commit()

# 查询
# all() [<Stu 3>] 获取所有
# get() 获取一个指定对象 通过主键查询
# first() <Stu 3>

# filter_by
# res = Stu.query.filter_by(sname='lisisi').first()

# 获取不到数据,则返回404
# res = Stu.query.get_or_404(7)

# res = Stu.query.filter(Stu.email.endswith('@qq.com')).all()
# res = Stu.query.filter(Stu.email.startswith('l')).all()

# res = Stu.query.limit(2).all()
# 跳过两条取两条 (limit 2,2)
# res = Stu.query.limit(2).offset(2).all()
# print(res)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:123456@127.0.0.1:3306/myapp"
# 如果设置成 True (默认情况),Flask-SQLAlchemy 将会追踪对象的修改并且发送信号。
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True

# 绑定app至SQLAlchemy
db = SQLAlchemy(app)

class Users(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
userinfo = db.relationship('UserInfo', backref='usersa', cascade="all, delete,delete-orphan",uselist=False)

class UserInfo(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(50))
users_id = db.Column(db.Integer, db.ForeignKey('users.id'))

if __name__ == "__main__":
db.create_all()

# 添加数据
# 创建用户
# u = Users(name = '赵六')
# db.session.add(u)
# db.session.commit()

# # 创建用户详情
# ui = UserInfo(email = 'zl@qq.com',users_id = u.id)
# db.session.add(ui)
# db.session.commit()

# 查询
# 通过用户 获取详情信息
# u = Users.query.first()
# print(u.name)
# print(u.userinfo.email)

# 通过用户详情 获取用户数据
# ui = UserInfo.query.first()
# print(ui.email)
# print(ui.users_id)
# print(ui.usersa.name)

# 删除
# u = Users.query.first()
# db.session.delete(u)
# db.session.commit()
  • 一对多(one-to-many)关系,在项目下的modelsORM.py文件输入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#分类
class Types(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
goodsa = db.relationship('Goods',backref="goodsb")
# stus = db.relationship('Stus',backref="classs",cascade="all, delete,delete-orphan")
# def __repr__(self):
# return '<Classs %r>' % self.cname
# 商品
class Goods(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50))
types_id = db.Column(db.Integer, db.ForeignKey('types.id'))
# def __repr__(self):
# return '<Stus %r>' % self.uname

# 一对多关系
# t = Types(name='手机')
# db.session.add(t)
# db.session.commit()

# g1 = Goods(title='魅族手机',types_id=t.id)
# g2 = Goods(title='小米手机',types_id=t.id)
# db.session.add(g1)
# db.session.add(g2)
# db.session.commit()

# 通过 分类 获取商品
# t = Types.query.first()
# print(t.name)
# print(t.goodsa)

# 通过商品 获取 分类
# g = Goods.query.first()
# print(g.title)
# print(g.goodsb.name)

# 删除
t = Types.query.first()
db.session.delete(t)
db.session.commit()
  • 多对多(many-to-many)关系,在项目下的modelsORM.py文件输入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
tags = db.Table('post_tag',
db.Column('tag_id', db.Integer, db.ForeignKey('tags.id')),
db.Column('post_id', db.Integer, db.ForeignKey('posts.id'))
)
#文章
class Posts(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255))
tags = db.relationship('Tags',secondary=tags,backref='Posts')
#标签
class Tags(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
# def __repr__(self):
# return self.name

# # 创建文章
# p1=Posts(title='flask开发')
# p2=Posts(title='django开发')
# p3=Posts(title='web框架开发博客系统')

# # 创建标签
# t1=Tags(name='falsk')
# t2=Tags(name='django')
# t3=Tags(name='web框架')

# # 添加数据
# p1.tags = [t1,t3]
# p2.tags = [t2,t3]
# p3.tags = [t3]

# db.session.add(p1)
# db.session.add(p2)
# db.session.add(p3)
# db.session.commit()

# 根据文章查标签
# p = db.session.query(Posts).first()
# print(p)
# print(p.title)
# print(p.tags)

# 根据标签查文章
# t = db.session.query(Tags).get(7)
# print(t)
# print(t.name)
# print(t.Posts)