NSP
NSP

Reputation: 1243

How to pass id from flask app to mysql database

I have a flask application that is connected to the MySQL database.

NOTE

database name = evaluation

table name = evaluation

columns = eval_id, eval_name, date

I have an 'evaluation table' with field eval_id, eval_name and date in it.

 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    |       |
    +-----------+-------------+------+-----+---------+-------+

How can I write an API to get a particular evaluation by its id? I tried the below, but it doesn't work.

@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)})

How can I correct this and get only the evaluation based on the eval_id mentioned in the app.route.

Upvotes: 0

Views: 971

Answers (2)

Egalicia
Egalicia

Reputation: 723

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

Upvotes: 0

Ori a
Ori a

Reputation: 324

You need to place the eval_id not as String but as a 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)})

Upvotes: 1

Related Questions