首页 > 解决方案 > When i am going to fectch data from mysql database table it gives output in the fomt of {coloumn_name:data}

问题描述

This Is My Python Code

    @app.route('/customer')
    def admin_customer():
        cur=mysql.connection.cursor()
        res=cur.execute("SELECT NUM,USERNAME,USEREMAIL,MOBILE FROM customertable")
        cur.execute("SELECT * FROM customertable")
        data = cur.fetchall()
        print(data)
        return render_template('users.html', data=data)

My Html code

{% for row in data %}

    <tr>
        <td>{{row.0}}</td>
        <td>{{row.1}}</td>
        <td>{{row.2}}</td>
    </tr>

{%endfor%}

output photo My Output photo

标签: pythonmysqlflask

解决方案


我不确定您面临什么问题,因为您没有向我们显示任何错误。但是,就我而言,这是mysql图书馆的预期行为。要正确呈现您的列表,请按名称访问字典键:

{% for row in data %}
    <tr>
        <td>{{row.USERID}}</td>
        <td>{{row.USERNAME}}</td>
        <td>{{row.USEREMAIL}}</td>
    </tr>
{%endfor%}

推荐阅读