通过用户模型,对数据库进行增删改查操作
时间:2021-07-01 10:21:17
帮助过:22人阅读
flask
import Flask
from flask_sqlalchemy
import SQLAlchemy
import connect
app = Flask(
__name__)
app.config.from_object(connect)#app连接数据库
db =
SQLAlchemy(app)
class User(db.Model):
__tablename__ =
‘user1‘
id = db.Column(db.Integer,primary_key=True,autoincrement=
True)
username = db.Column(db.String(20),nullable=
False)
password = db.Column(db.String(20), nullable=
False)
againpass = db.Column(db.String(20), nullable=
False)
#db.create_all()
#增加
user=User(username=
‘123456‘,password=
‘123456789‘)
db.session.add(user)
db.session.commit()
#查找
user=User.query.filter(User.username==
‘two1234‘).first()
print(user.id,user.username,user.password)
#修改
user=User.query.filter(User.username ==
‘mis1234‘).first()
user.password =
‘22222‘
db.session.commit()
print(user.id,user.username,user.password)
#删除
user=User.query.filter(User.username==
‘two1234‘).first() user.password=
‘12345678910‘
db.session.delete(user)
db.session.commit()
@app.route(‘/‘)
def hello_world():
return ‘Hello World!‘
通过用户模型,对数据库进行增删改查操作
标签:let hello first integer str connect mit 连接数据库 session