数据入库
用的数据库是老师给的map_cy.sql文件,导入workbench
workbench中如何导入.sql文件 - BAHG - 博客园
map_cy.sql:
链接: https://pan.baidu.com/s/1c_5ynK4Q6jFNcKXPW9NgcQ 提取码: 67sp
--来自百度网盘超级会员v6的分享
最开始导入的时候一直报以上1064错误,检索了很久,发现是workbench版本不一,实现的select语句格式不一样导致的,然后我就手动改了四十几行的select…………电脑卡爆
创建文件夹
需要按照这个框架创建文件夹,app下放置templates文件夹和app.py,index放到templates里。
代码实现
app.py
from flask import Flask,render_template,request
import pymysql
app=Flask(__name__)
@app.route('/')
def index():
db = pymysql.connect(host='localhost', user='root', password='888888', db='sys', charset='utf8')
cursor = db.cursor()
try:
cursor.execute("select * from sys.map_cy")
rs=cursor.fetchall()
rs=(list)(rs)
rscount=len(rs)
except:
print('error')
db.close()
return render_template("index.html",rscount=rscount,rs=rs)
if __name__ == '__main__':
app.run(debug=True)
新手入门flask的时候比较容易卡在调用数据库上,主要是两个方面:
- 要记得数据库的密码,如果连接失败很大原因是password错了= = 在workbench里自行检查下密码就行
- 版本的select语句会不一样(不知道是否可以设置),反正我的必须要写上schema名才能成功select到,也是可以在workbench里自行检查的
index.htm
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flask+成语</title>
</head>
<body>
<h1>成语词典</h1>
<!--{{rs}}-->
{% for r in rs %}
{{r}} <br>
{% endfor %}
</body>
</html>
我用的pycharm,最后run app.py,点进127进去就可以啦
最终结果