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
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.