-1

I am relatively new to flask and I am having some issues with displaying my database as a html table. I don't know if this might be a stupid question, but I don't know how to get the values from this array.

cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute("SELECT CardNo, CardName, CardExpDate, CardSecretCode FROM carddetails WHERE UID=%s", (session['id'],))
    rows = cursor.fetchall()# data from database
    for row in rows:
        print(row)
        for i in row:
            print(i)
{'CardNo': '4561441635108144', 'CardName': 'Zachery Williamson', 'CardExpDate': '12-25', 'CardSecretCode': '219'}
CardNo
CardName
CardExpDate
CardSecretCode
{'CardNo': '4590618204792680', 'CardName': 'SAM LETTUCE', 'CardExpDate': '03-23', 'CardSecretCode': '440'}
CardNo
CardName
CardExpDate
CardSecretCode

I want to get the values next to the colon for each of them, but I don't know why it only returns the first bit which is useless for my purpose. My idea is implementing those values in a html table, but I am struggling with it. By the way, tha card information displayed above is completely random.

Manu
  • 11
  • 2

1 Answers1

0
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("SELECT CardNo, CardName, CardExpDate, CardSecretCode FROM carddetails WHERE UID=%s", (session['id'],))
rows = cursor.fetchall()# data from database
for row in rows:
    print(row)
    for i in row:
        print(i, row[i])

With for i in row, you iterate over a dictionary and i is a key for each iteration. To get the associated value, you have to get it like for all dictionaries in Python row[i].

clemsciences
  • 121
  • 9