Research for a day because of the need to get data from the database and then converted into json format out to expose API
Found a piece of data is easy, two more than I actually do for so long at any rate out first posted about the update later
mysql of the operation is relatively easy https://pynative.com/python-mysql-select-query-to-fetch-data/
flask also a lot easier to reference
Mysql extraction field name but is the least seen - " https://www.jianshu.com/p/057a784febb9
= cur.description Fields # to get the field name in the query results column, SQL is used aliases If the query shown here aliases. cur.close () conn.Close () # the Main the column_list = [] # define a list of field names for I in Fields: column_list.append (I [0]) # extract field names, append to the list # Print # listing the column_list display results: [ 'id', 'NAME ', 'LOCAL', 'mobile', 'CreateTime']
select out of the list and the field names combined to form a dict
https://blog.csdn.net/weixin_41104835/article/details/89000096
for row in Result: # define Python Dictionary Data = {} # The row each element, is added to the dictionary. for I in Range (len (the column_list)): Data [the column_list [I]] = Row [I] # Data [the column_list [0]] = Row [0] # # the Python field format and json field format conversion # Data [the column_list [. 1]] = STR (Row [. 1]) # Data [the column_list [2]] = STR (Row [2]) # Data [the column_list [. 3]] = STR (Row [. 3]) ------ --------------- author: Come Ye have spread source: CSDN original: HTTPS: //blog.csdn.net/weixin_41104835/article/details/89000096 Disclaimer: This article is a blogger original article, reproduced, please attach Bowen link!
A lot of people refer to code
https://www.cnblogs.com/yufeihlf/p/6004124.html - but this one's own header to knock too much trouble
# Converting the data into a tuple list of types, each type of data element dictionary is: [{ 'field 1': 'value field 1', '2 field': 'field value of 2', ..., ' field N: field value N '}, {} the second data, ..., N-th data {}] for Row in data: Result = {} Result [ ' nm ' ] = Row [0] Result [ ' DC ' ] = Row [. 1 ] Result [ ' IU ' ] = Row [2 ] Result [ ' IT ' ] = STR (Row [. 3 ]) Result [ ' OG '] = str(row[4]) result['mt'] = str(row[5]) result['pn'] = row[6] result['du'] = row[7] result['am'] = row[8] result['mc'] = str(row[9]) result['vc'] = str(row[10]) result['vn'] = row[11] result[''sm] = Row [12 is ] Result [ ' SE ' ] = STR (Row [13 is ]) Result [ ' AO ' ] = STR (Row [14 ]) jsonData.append (Result) Print U ' into a list of raw data dictionaries : ' , jsonData
My ultimate goal is Call http://127.0.0.1:5000/north/getSpace time to return json value of my space
But not optimized to fully realize my request. . One day come up pretty well limited level
1 #!flask/bin/python 2 # coding=utf-8 3 from flask import Flask, jsonify 4 import json 5 import mysql.connector 6 from mysql.connector import Error 7 8 app = Flask(__name__) 9 10 11 12 try: 13 connection = mysql.connector.connect(host='127.0.0.1',database='bms_1',user='root',password='Pxxxx') 14 if connection.is_connected(): 15 SQL = "select id,guid,tag,path,name,node_type,space_type,parent_guid,time from space;" 16 cursor = connection.cursor() 17 cursor.execute(SQL) 18 result_list = cursor.fetchall() #return sql result 19 print("fetch result-->",type(result_list)) #is s list type, need to be a dict 20 21 fields_list = cursor.description # sql key name 22 print("fields result -->",type(fields_list)) 23 #print("header--->",fields) 24 cursor.close() 25 connection.close() 26 27 28 29 # main part 30 column_list = [] 31 for i in fields_list: 32 column_list.append(i[0]) 33 print("print final colume_list",column_list) 34 is 35 # Print ( "colume List ->", the column_list) 36 37 [ jsonData_list = [] 38 is for Row in result_list: 39 data_dict = {} 40 for I in Range (len (the column_list)): 41 is data_dict [the column_list [I ]] = Row [I] 42 is # the data_dict jsonData_list returned to join the list 43 is jsonData_list.append (data_dict) 44 is # Print U 'into a list of raw data dictionary:', jsonData_list 45 46 is 47 @ app.route ( ' / north / getSpace', methods=['GET']) 48 def get_space(): 49 return jsonify({'space': jsonData_list}) 50 51 52 if __name__ == '__main__': 53 app.run(debug=True) 54 55 56 except Error as e: 57 print("Error while connection to Mysql", e) 58 finally: 59 connection.close() 60 print "==== mysql closed==="