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/[email protected]/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