如何将 id 从 flask app 传递到 mysql 数据库

How to pass id from flask app to mysql database

我有一个连接到 MySQL 数据库的烧瓶应用程序。

NOTE

database name = evaluation

table name = evaluation

columns = eval_id, eval_name, date

我有一个 'evaluation table',其中包含字段 eval_id、eval_name 和日期。

 mysql> use evaluation;
    mysql> describe evaluation;
    +-----------+-------------+------+-----+---------+-------+
    | Field     | Type        | Null | Key | Default | Extra |
    +-----------+-------------+------+-----+---------+-------+
    | eval_id   | int(11)     | NO   | PRI | NULL    |       |
    | eval_name | varchar(20) | NO   |     | NULL    |       |
    | date      | datetime(6) | NO   |     | NULL    |       |
    +-----------+-------------+------+-----+---------+-------+

如何编写 API 以通过其 id 获得特定评估? 我尝试了以下方法,但它不起作用。

@app.route('/getEval/<int:eval_id>', methods=['GET'])
def getEvalByID(eval_id):
    cur.execute('''select * from evaluation.evaluation where eval_id=eval_id''')
    res = cur.fetchall()
    return jsonify({'test':str(res)})

如何更正此问题并仅获得基于 app.route 中提到的 eval_id 的评估。

您需要将 eval_id 放置为 VAR 而不是字符串。

@app.route('/getEval/<int:eval_id>', methods=['GET'])
def getEvalByID(eval_id):
    cur.execute('select * from evaluation.evaluation where eval_id=' + str(eval_id))
    res = cur.fetchall()
    return jsonify({'test':str(res)})

试试 cur.execute('select * from evaluation.evaluation where eval_id={}'.format(eval_id))