美文网首页程序员
Python写web接口(与mysql交互)

Python写web接口(与mysql交互)

作者: GG_lyf | 来源:发表于2021-02-21 12:05 被阅读0次

    前言

      之前总是写java的crud,写的实在厌烦,因此这次写个Python的(只有查)


      1.


    数据库

      2. Python代码

    import pymysql
    import json
    
    from flask import Flask
    
    app = Flask(__name__)
    
    
    @app.route('/index1/<int:id>', methods=['GET'])
    def indextest(id):
        # inputData = request.json.get('inputData')
        data1 = getcontent(id)
        return data1
    
    
    def getcontent(inputData):
        conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='python-mysql')  # db:表示数据库名称
        cur = conn.cursor()
        sql = "select * from book where vId='%d'" % (inputData)
        cur.execute(sql)
        data = cur.fetchone()
        print(data)
        result = {'vId': data[0], 'vN': data[1], 'cCnt': data[2]}
        return json.dumps(result, ensure_ascii=False, indent=4)
    
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5590)
    
    

    访问http://127.0.0.1:5590/index1/5

    googe

    相关文章

      网友评论

        本文标题:Python写web接口(与mysql交互)

        本文链接:https://www.haomeiwen.com/subject/zsrzxltx.html