安装
sudo apt-get update sudo apt-get install -y mysql
sudo dpkg -i mysql-apt-config*
设置数据库安全性,根据需求填写,注意对于mysql8来说只有2类密码(数字,大写字母,小写字母,下划线,大于8位)才能通过Python/java,以非sudo权限用户访问到数据库
sudo mysql_secure_installation
sudo dpkg -i mysql-workbench-community_*
再安装mysql-workbench-community-dbgsym
sudo dpkg -i mysql-workbench-community-dbgsym*
sudo mysql #下面进入mysql> mysql> alter user 'root'@'localhost' identified with mysql_native_password by '新密码'; mysql> exit; #退出mysql后重启服务 sudo service mysql restart
conda create -n name flask flask-login flask-sqlalchemy flask-wtf mysqlclient pymysql python=3.8 #或者在base环境或者需要使用的环境下执行 conda install -y flask flask-login flask-sqlalchemy flask-wtf mysqlclient pymysql
from flask import Flask,render_template,send_from_directory app = Flask(__name__) @app.route('/') def index(): return render_template('index.htm') @app.route('/<path:filename>') def custom_static(filename): return send_from_directory(directory='templates/',filename=filename) app.run()
flask run
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>test</title> <meta name="description" content="你的作品"> <meta name="author" content="你的名字"> </head> <body> {% for row in results %} {% for column in dct[i].items() %} <p style="text-align: left;"> {{column[1]}}:{{column[2]}<br> </p> {% endfor %} {% endfor %} </body> </html>
@app.route('/test.html') def sql(): import mysql db = pymysql.connect(host="127.0.0.1", user="root",password="改成你的密码",database="改成你的数据库",charset="utf8") cursor = db.cursor() sql="你的数据库查询语句" cursor.execute(sql) results = cursor.fetchall() db.close() return render_template('test.html',results=results)