增加: user = db.session.add(user) db.session.commit()
查询: User.query.filter(User.username == 'mis1114').first()
修改: user.password = '111111' db.session.commit()
删除: db.session.delete(user) db.session.commit()
SQLALCHEMY_DATABASE_URI='mysql+pymysql://root:@localhost:3306/mis15?charset=utf8'SQLALCHEMY_TRACK_MODIFICATTONS = False
from flask import Flaskfrom flask_sqlalchemy import SQLAlchemyimport configapp = Flask(__name__)app.config.from_object(config)db=SQLAlchemy(app)class User(db.Model): __tablename__ = 'User' 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) nickname = db.Column(db.String(20))#db.create_all()#添加user=User(username='hehe',password='1234567')db.session.add(user)db.session.commit()#删除user=User.query.filter(User.username=='haha').first()user.password='2345'db.session.delete(user)db.session.commit()#修改user=User.query.filter(User.username=='hehe').first()user.password='2345'db.session.add(user)db.session.commit()#查找user=User.query.filter(User.username=='hehe').first()print(user.id,user.username,user.password)@app.route('/')def hello_world(): return 'Hello World!'if __name__ == '__main__': app.run()