Python Database Read Operation

The SELECT statement is used to read the values from the databases. We can restrict the output of a select query by using various clause in SQL like where, limit, etc.

Python provides the fetchall() method returns the data stored inside the table in the form of rows. We can iterate the result to get the individual rows.

Here, we will extract the data from the database and We will also format the output to print it on the console.

Python Fetch Database Table Entries

import mysql.connector  
myconn = mysql.connector.connect(host = "localhost", user = "root",password = "naveen",database = "PythonDB")  
cur = myconn.cursor()  
try:  
    cur.execute("select * from Employee")  
    result = cur.fetchall()  
      
    for x in result:  
        print(x);  
except:  
    myconn.rollback()
myconn.close()