Q:

Python program to search for a record using ID in SQL

belongs to collection: Python Database (SQL/MySQL) Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python program to search product records based on ... >>
<< Search a record from the database table using patt...