There are several methods available which includes using python libraries sqlalchemy
or pandas
. However, for the method I describe here, you need to have the following Python libraries installed.
Extracting Fields Names of an HTML form - Python
Dynamically serving a matplotlib image to the web using python
Let's assume you have read the user input and the two dates stored in the variables st_date
and end_date
. Here is the code.
import cx_Oracle
import matplotlib.pyplot as plt
query = 'SELECT order_date, count(order_id) order_count
FROM orders WHERE order_date BETWEEN ' + st_date + ' AND ' + end_date +
' GROUP BY order_date '
order_date = []
order_count = []
#connect to database and execute query.
db = cx_Oracle.connect('user', 'password', 'localhost:1521/XE')
cursor = db.cursor()
cursor.execute(query)
#loop through the rows fetched and store the records as arrays.
for row in cursor:
order_date.append(row[0])
order_count.append(row[1])
#plot the bar chart
plt.bar(order_date,order_count)