Home > Enterprise >  Python flask_mysqldb passing over 10 does't work
Python flask_mysqldb passing over 10 does't work

Time:10-20

I'm totally new and learning Python MySQL, And I have an issue with passing over 10 to %s

@app.route('/usr/<id>', methods=['GET'])
def selected_logs(id):
    response_object = {'status': 'success'}
    cur = mysql.connection.cursor()
    cur.execute('''
    SELECT
            usr.id,
            usr.corp_id,
            date_format (log.date, '%%m/%%d/%%Y') AS date
        FROM 
            (usr
            INNER JOIN corps ON corps.id = usr.corp_id)
        WHERE
            usr.corp_id = %s
        ORDER BY usr.id DESC;
    ''', (id))
    results = cur.fetchall()
    response_object['usr'] = results
    return jsonify(response_object)

In the URL http://localhost:5000/usr/9 works, but start to 10... won't work, please help me.

MySQLdb._exceptions.ProgrammingError: not all arguments converted during bytes formatting

THANK YOU SO MUCH

CodePudding user response:

Execute() takes an iterable as the second argument. You need to pass an iterable of values to bind to parameters, (id) is not an iterable. It is a common mistake to think that it is a tuple but it is not, (id,) is a tuple which is an iterable.

cur.execute('''
    SELECT
            usr.id,
            usr.corp_id,
            date_format (log.date, '%%m/%%d/%%Y') AS date
        FROM 
            (usr
            INNER JOIN corps ON corps.id = usr.corp_id)
        WHERE
            usr.corp_id = %s
        ORDER BY usr.id DESC;
    ''', (id,))

The error hides this issue because the id value is '10' and strS are iterables in Python.

MySQLdb._exceptions.ProgrammingError: not all arguments converted during bytes formatting

CodePudding user response:

By reading the error im asumming the id value you are recibing is not the correct data type you need for the evaluation in your query.

  • Related