[Python] How to make tuple type SQL results to be ready to display in Flask?

Discussão em 'Python' iniciado por Stack, Julho 24, 2025.

  1. Stack

    Stack Membro Participativo

    A typical query such as this:

    conn=sql.connect('./database.db')
    cursor=conn.cursor()
    cursor.execute('SELECT year, unit, facilities, workforce, vehicles FROM units WHERE unit = ?', (unit,))
    unit = cursor.fetchall()
    conn.close()


    works right to result whatever is needed from the database.

    However when it comes to passing this "unit" variable to template page as it is in Flask as it is to iterate and display in a table with this ending in route;

    return render_template('analysis.html', unit=unit)


    I get an error:


    TypeError: int() argument must be a string, a bytes-like object or a real number, not 'tuple'
    ​

    I have tried to convert the whole thing to a list with this:

    unit = list(unit)


    The error has not changed.

    Also I have tried to dump it as json with this;

    unit = json.dumps(unit)


    Flask returned this error:


    ValueError: invalid literal for int() with base 10: '['
    ​

    So I thought there should be some workaround to convert my multiline tuple result, to have it displayed, with this basic given database connection scheme. Your help is very much appreciated.

    Continue reading...

Compartilhe esta Página