How to Search One Record from Database Table with 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")
    empid=int(input("Enter Employee id for search:"))
    sql="select * from employee where eid=%d"
    cur=con.cursor()
    cur.execute(sql % empid)
    result=cur.fetchone()
    if(cur.rowcount==0):
        print("--------NO RECORD FOUND-----------------")
    else:
        print("ID      NAME   DEPARTMENT   SALARY")
        print("%d      %s       %s      %d"%(result[0],result[1],result[2],result[3]))
except Exception as obj:
    print(obj)
finally:
    con.close()
    print("Connection close")

 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

Leave a Reply

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