Python Format Database Result

We can format the result by iterating over the result produced by the fetchall() or fetchone() method of cursor object since the result exists as the tuple object which is not readable.

Format Database Result

In this example, we will format the results returned by the database

CopiedCopy Code

import mysql.connector  
myconn = mysql.connector.connect(host = "localhost", user = "root",password = "naveen",database = "PythonDB")  
cur = myconn.cursor()  
  
try:  
    cur.execute("select name, id, salary from Employee")  
    result = cur.fetchall()  
  
    print("Name    id    Salary");  
    for row in result:  
        print("--s    --d    --d"--(row[0],row[1],row[2]))  
except:  
    myconn.rollback()    
myconn.close()