赞
踩
这里首先要安装两个库
- pip install flask_sqlalchemy
- pip install mysqlclient
1. 启动文件(负责启动)
2. 核心文件
3. 路由注册文件
如下结构:
templates放模板
application.py为核心文件
controller.py放请求控制端
manager.py放启动文件
www.py放路由注册文件
程序运行截图如下:
源码如下:
index.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <p>Flask 查询数据库</p>
- {% for item in result %}
- {{item['User']}}
- {% endfor %}
- </body>
- </html>
application.py
- from flask import Flask
- from flask_sqlalchemy import SQLAlchemy
-
- app = Flask(__name__)
-
- app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:XXXXXXX!@122.XX.XXX.141/mysql"
- db = SQLAlchemy(app)
controller.py
- from flask import Flask,Blueprint,request,make_response,jsonify,render_template
- from sqlalchemy import text
- from application import db
-
- index_page = Blueprint("index_page", __name__)
-
- @index_page.route("/sql")
- def sqlQuery():
- context = {}
- sql = text("select * from user")
- result = db.engine.execute(sql)
- 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")
- from application import app
- from controller import index_page
-
- app.register_blueprint(index_page, url_prefix = "/it1995")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。