flask+sql查询
# -*- coding: utf-8 -*-

import pymysql

  

from flask import Flask, request

from flask_cors import CORS

  

app = Flask(__name__)

CORS(app)

  
  

@app.route('/', methods=['GET'])

def hello():

    args = request.args  # 获取get参数

    wd = args.get("wd")

    result = []

  

    db = pymysql.connect(host='localhost',

                         user='root',

                         password='lylelove2000A',

                         database='jinghe')

    # 使用cursor()方法获取操作游标

    cursor = db.cursor()

  

    sql = "SELECT * FROM community WHERE work LIKE BINARY '%"+wd+"%'"

  

    try:

        # 执行SQL语句

        cursor.execute(sql)

        # 获取所有记录列表

        results = cursor.fetchall()

        for row in results:

            id = row[0]

            name = row[1]

            address = row[2]

            work = row[3]

            birthday = row[4]

            workplace = row[5]

            salary = row[6]

            # 打印结果

            # result.append({"id": id, "name": name, "address": address, "work": work,"birthday":birthday,"workplace":workplace,"salary":salary})

            result.append({"id": id, "name": name})

    except:

        print("Error: unable to fetch data")

    return result

  
  

if __name__ == '__main__':

    app.run(debug=True)