Python program to search for a record using ID in SQL

Here, we will see a Python program to search for a record using ID in SQL.
Submitted by Shivang Yadav, on March 06, 2021

We are given a table consisting of employee records. And we need to search for an employee's details using an ID entered by the user.

The employee table consists of the following details:

  1. Employee Name
  2. Salary
  3. Designation
  4. City
  5. Birth Date

Program to search for a record using ID in Python

import pymysql as mysql

try:
    db=mysql.connect(host="localhost",port=3306,user="root",password='123',db="lg")
    cmd=db.cursor()

    id=input("Enter Id you Want to Search?")

    q="Select * from employee where employeeid={0}".format(id)
    cmd.execute(q)

    row=cmd.fetchone()
    if(row):
     print("Employee Name:",row[1])
     print("Salary:", row[2])
     print("Designation:", row[3])
     print("City:", row[4])
     print("Birth Date:", row[5])
    else:
     print("Record Not Found....")

    db.close()
except Exception as e:
    print(e)

Output:

Enter Id you Want to Search?23
Employee Name:John
Salary:45000
Designation:Senior Developer
City:London
Birth Date:12/4/1988

Python database (SQL/MySQL) programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.