Skip to content
Advertisement

How would I display the result of a SQL query in a readable way?

I couldn’t find a solution on how to easily format the result of a query.

cursor.execute("SELECT name, email FROM customers")
print(cursor.fetchone())
>>> [('Joe Bloggs', 'joebloggs@jbloggs.com')]

I would like the result to be formatted in a way like:

  • Name : Joe Bloggs
  • Email : joebloggs@jbloggs.com

Advertisement

Answer

row = cursor.fetchone()[0] #get the only element in array
name = row[0]
email = row[1]

And then you can use name and email to format your result. You also need to look out for when your query returns 0 elements.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement