Python Program to Display Employee Details

The below is an example of a Python program to display employee details. In this program, it will ask the user to input employee number, then it will query the database table for that employee and will print the details. Also, a user can exit the program if he enters the 0 instead of an employee number. The database used for this example is Oracle, and the table is employees of the HR schema.

You can download the HR schema script from the following link Download HR Schema.

Python Program to Display Employee Details From Oracle Table

import cx_Oracle

con = cx_Oracle.connect('hr/hrpsw@localhost/orcl')

def display_emp(n_empno):
    cur = con.cursor()
    cur.execute("select first_name, last_name, phone_number, hire_date, salary from employees where employee_id = :n_empno", {'n_empno': (n_empno)})
    for fname, lname, phnumber, hdate, sal in cur:
        print('First Name: ', fname)
        print('Last Name: ', lname)
        print('Phone: ', phnumber)
        print('Hire Date: ', hdate)
        print('Salary: ', sal)
    cur.close()

while True:
    empno = input('Enter Employee Number: (enter 0 to exit)')
    if empno == '0':
        break
        con.close()
    else:
        display_emp(empno)

Output

Enter Employee Number: (enter 0 to exit)102
First Name: Lex
Last Name: De Haan
Phone: 515.123.4569
Hire Date: 2001-01-13 00:00:00
Salary: 17000.0
Enter Employee Number: (enter 0 to exit)

See also:

Python Program to Insert Record in Oracle Table

Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 20 years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.

2 Comments

  1. Sir I want an example of a python file where data can be fetched from Oracle table and displayed in HTML's <TABLE> format

    • Thank you for your reply. This is working fine. But my actual query is : I have created a simple Flask project. I have to different files, one is view.py and other is view.html. I want to fetch data from a oracle table through python program and display the result in html file. Please help.

Comments are closed.