关键词搜索

源码搜索 ×
×

Flask笔记-通过Model访问数据库

发布2020-02-27浏览4919次

详情内容

这个类似与Java的MyBatis

这样的话开发就快速很多了!

程序运行截图如下:

程序结构如下:

源码如下:

account.py

  1. from application import db
  2. class Account(db.Model):
  3. id = db.Column(db.Integer, primary_key = True)
  4. name = db.Column(db.String(50))
  5. password = db.Column(db.String(50))

index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <p>Flask 查询数据库</p>
  9. <p>
  10. {% for item in result %}
  11. {{item['id']}} {{item['name']}} {{item['password']}}
  12. {% endfor %}
  13. </p>
  14. </body>
  15. </html>

application.py

  1. from flask import Flask
  2. from flask_sqlalchemy import SQLAlchemy
  3. app = Flask(__name__)
  4. app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:XXXXXXXXX@122.XXX.XXX.XXX/mytest"
  5. db = SQLAlchemy(app)

controller.py

  1. from flask import Flask,Blueprint,request,make_response,jsonify,render_template
  2. from sqlalchemy import text
  3. from application import db
  4. from common.models.account import Account
  5. index_page = Blueprint("index_page", __name__)
  6. @index_page.route("/sql")
  7. def sqlQuery():
  8. context = {}
  9. result = Account.query.all()
  10. context["result"] = result
  11. return render_template("index.html", **context)

manager.py

  1. from application import app
  2. from www import *
  3. if __name__ == "__main__":
  4. app.run(host = "0.0.0.0", debug = "True")

www.py

  1. from application import app
  2. from controller import index_page
  3. app.register_blueprint(index_page, url_prefix = "/it1995")

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载