这个类似与Java的MyBatis。
这样的话开发就快速很多了!
程序运行截图如下:
程序结构如下:
源码如下:
account.py
- from application import db
-
- class Account(db.Model):
- id = db.Column(db.Integer, primary_key = True)
- name = db.Column(db.String(50))
- password = db.Column(db.String(50))
index.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <p>Flask 查询数据库</p>
- <p>
- {% for item in result %}
- {{item['id']}} {{item['name']}} {{item['password']}}
- {% endfor %}
- </p>
- </body>
- </html>
application.py
- from flask import Flask
- from flask_sqlalchemy import SQLAlchemy
-
- app = Flask(__name__)
-
- app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:XXXXXXXXX@122.XXX.XXX.XXX/mytest"
- db = SQLAlchemy(app)
controller.py
- from flask import Flask,Blueprint,request,make_response,jsonify,render_template
- from sqlalchemy import text
- from application import db
- from common.models.account import Account
-
-
-
- index_page = Blueprint("index_page", __name__)
-
- @index_page.route("/sql")
- def sqlQuery():
- context = {}
- result = Account.query.all()
- context["result"] = result
- return render_template("index.html", **context)
manager.py
- from application import app
- from www import *
-
- if __name__ == "__main__":
- app.run(host = "0.0.0.0", debug = "True")
www.py
- from application import app
- from controller import index_page
-
- app.register_blueprint(index_page, url_prefix = "/it1995")