How to Search Records from Database Table using PDBC

Python course with 57 real-time projects - Learn Python

Program 1

import MySQLdb
from MySQLdb import *
try:
    con=MySQLdb.Connect(host="localhost",user="root",password="root",database="dataflair")
    print("Connection Success")
    sql="select * from employee"
    cur=con.cursor()
    cur.execute(sql)
    result=cur.fetchall()
    print("----------------------------------------------------------------")
    print("ID         NAME       DEPARTMENT     SALARY")
    for row in result:
        print("%d        %s           %s           %d" %(row[0],row[1],row[2],row[3]))
        print("-------------------------------------------------------------")
except Exception as obj:
    print(obj)
finally:
    con.close()

 

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

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