通过用户模型,对数据库进行增删改查操作。
时间:2021-07-01 10:21:17
帮助过:20人阅读
from flask
import Flask
2 from flask_sqlalchemy
import SQLAlchemy
3 import config
4
5 app = Flask(
__name__)
6 app.config.from_object(config)
7
8 db=
SQLAlchemy(app)
9
10 class User(db.Model):
11 __tablename__ =
‘User‘
12 id = db.Column(db.Integer,primary_key=True,autoincrement=
True)
13 username = db.Column(db.String(20),nullable=
False)
14 password = db.Column(db.String(20),nullable=
False)
15 nickname = db.Column(db.String(20
))
16 #db.create_all()
17
18 #增
19 user=User(username=
‘liu1234‘,password=
‘123456789‘)
20 db.session.add(user)
21 db.session.commit()
22 #删
23 user=User.query.filter(User.username==
‘two1234‘).first() user.password=
‘12345678910‘
24 db.session.delete(user)
25 db.session.commit()
26 #改
27 user=User.query.filter(User.username==
‘two1234‘).first()
28 user.username=
‘three1234‘
29 db.session.add(user)
30 db.session.commit()
31 #查
32 user=User.query.filter(User.username==
‘two1234‘).first()
33 print(user.id,user.username,user.password)
34
35 @app.route(
‘/‘)
36 def hello_world():
37 return ‘Hello World!‘
38
39
40 if __name__ ==
‘__main__‘:
41 app.run()
增:
删:
改:
查:
通过用户模型,对数据库进行增删改查操作。
标签:query class word com integer pre bsp use creat