Skip to content

Commit 536ff6f

Browse files
committed
display script added - to display data from database
1 parent 31de4f7 commit 536ff6f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Udemy Scraper/display.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sqlite3
2+
from sqlite3 import Error
3+
4+
# Function to connect to the SQL Database
5+
def sql_connection():
6+
try:
7+
con = sqlite3.connect('udemyDatabase.db')
8+
return con
9+
except Error:
10+
print(Error)
11+
12+
con = sql_connection()
13+
14+
# Function to Fetch courses from database
15+
def sql_fetch(con):
16+
cursorObj = con.cursor()
17+
try:
18+
cursorObj.execute('SELECT * FROM courses') # SQL search query
19+
except Error:
20+
print("Database empty... Fetch courses using fetcher script")
21+
return
22+
23+
rows = cursorObj.fetchall()
24+
25+
# Print table header
26+
print("{:^30}".format("Title"),"{:^30}".format("Description"),"{:^20}".format("Instructor"),
27+
"{:<15}".format("Current Price"),"{:<18}".format("Original Price"),"{:^10}".format("Rating"),
28+
"{:^10}".format("Hours"),"{:^10}".format("Lectures"))
29+
30+
# Print all rows
31+
for row in rows:
32+
# Format individual data items for printing in a table like manner
33+
title = "{:<30}".format(row[0] if len(row[0])<30 else row[0][:26]+"...")
34+
description = "{:<30}".format(row[1] if len(row[1])<30 else row[1][:26]+"...")
35+
instructor = "{:<20}".format(row[2] if len(row[2])<30 else row[2][:16]+"...")
36+
current_price = "{:^15}".format(row[3])
37+
original_price= "{:^18}".format(row[4])
38+
rating = "{:^10}".format(row[5])
39+
hours= "{:^10}".format(row[6])
40+
lectures = "{:^10}".format(row[7])
41+
print(title,description,instructor,current_price,original_price,rating,hours,lectures)
42+
43+
sql_fetch(con)

0 commit comments

Comments
 (0)