from flask import Flask
import json
import MySQLdb
app = Flask(__name__)
_db = MySQLdb.connect("localhost", "root", "12345678", "Lemoying", charset='utf8')
_cursor = None
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/week_price')
def week_price():
sql = '''SELECT coin_name FROM week_price LIMIT 10'''
_cursor = _db.cursor()
try:
_cursor.execute(sql)
row_headers = [x[0] for x in _cursor.description]
results = _cursor.fetchall()
print(row_headers)
json_data = []
for result in results:
json_data.append(dict(zip(row_headers, result)))
print(json_data)
# _db.commit()
except:
_db.rollback()
_cursor.close()
return json.dumps(json_data)
if __name__ == '__main__':
app.run()