ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.