How to Fetch All Record From Database

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Program 1

# Program for Select data(search) 
import MySQLdb
try:
    con=MySQLdb.connect(host='localhost',user='root',password='root',database='DataFlair')   
    #print("Data Base connected")
    sql="select * from employee"
    cur=con.cursor()
    cur.execute(sql)
    result=cur.fetchall()
    print("------------------------------------------")
    print("Emp Id\t Emp Name\t Mobile\t Age")
    print("------------------------------------------")
    for row in result:
        print("%d\t %s\t        %d\t  %d" %(row[0],row[1],row[2],row[3]))
    print("------------------------------------------")
except Exception as obj:
    print(obj)    
finally:
    con.close()

 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *