python 從數據庫查數據並轉為json


from flask import Flask,render_template
import pymysql
import json


app = Flask(__name__)

conn = pymysql.connect('localhost',user = "root",password = "123456",db = "testdb")
cursot = conn.cursor(pymysql.cursors.DictCursor)


@app.route('/')
def hello_world():
dicts = {'name': "lucy", "sex": "boy",'age':18}
dicss1 = [{'name': "lucy", "sex": "boy",'age':18},{'name': "lucy", "sex": "boy",'age':18}]
json_dic = json.dumps(dicss1)
return render_template("show1.html",json_dic = json_dic)

@app.route('/list')
def list():


cursot.execute("select Id,Name,Age,cast(AddTime as char) as date from news")
rows = cursot.fetchall()


#object_list = []
#for row in rows:
# result = {}
# result["Id"] = row[0]
# result["Name"] = row[1]
# result["Age"] = row[2]
# result["AddTime"] = row[3]
# object_list.append(result)


return render_template('show.html',res = json.dumps(rows))
cursot.close()
conn.close()




if __name__ == '__main__':
app.run()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM