flask使用Articel对mysql增删改查操作
时间:2022-08-30 来源:未知 投稿:admin 点击:次
例:
from flask import Flask,render_template from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) HOSTNAME = '58.218.204.78' PORT = '3306' DATABASE = 'fwqzx' USERNAME = 'fwqzx' PASSWORD = 'fwqzx123456' DB_URI = 'mysql+pymysql://{}:{}@{}:{}/{}?charset-utf8'.format(USERNAME,PASSWORD,HOSTNAME,PORT,DATABASE) app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True db = SQLAlchemy(app) class Article(db.Model): __tablename__ = "article" id = db.Column(db.Integer,primary_key=True,autoincrement=True) title = db.Column(db.String(200),nullable=False) content = db.Column(db.Text,nullable=False) db.create_all() @app.route("/") def index(): #增加一条数据 # articel = Article(title="我的宝宝",content="很可爱") # db.session.add(articel) # db.session.commit() #查询数据 # article = Article.query.filter_by(id=1)[0] # print(article.title) #改数据 # article = Article.query.filter_by(id=1)[0] # article.content = '确实很可爱' # db.session.commit() #删数据 Article.query.filter_by(id=1).delete() db.session.commit() if __name__ == "__main__": app.run(port=80)
如果您的问题仍未解决,还可以加入服务器在线技术交流QQ群:8017413寻求帮助。
相关内容