'Python partially outputs some values and fully outputs the last ones
Hello so I'm trying to get some data from different tables in SQLite3 using python But it only gives the data from all tables on the last entry, If you don't understand what i mean here are some code snippets and the output
Here's the code
import sqlite3
def getBookingFromUsername(name):
try:
conn = sqlite3.connect('database.db')
cur = conn.cursor()
cur.execute('SELECT * FROM bookings WHERE username = "{}"'.format(name))
rows = cur.fetchall()
for row in rows:
print("----------------Sessions------------------")
print("ID: "+str(row[0]))
print("Session ID: "+str(row[1]))
sessionID=row[1]
getSessionFromID(sessionID)
return sessionID
except:
print("Oops something went wrong 😓")
def getSessionFromID(sessionID):
try:
conn = sqlite3.connect('database.db')
cur = conn.cursor()
cur.execute('SELECT * FROM sessions WHERE id = "{}"'.format(sessionID))
rows = cur.fetchall()
for row in rows:
print("Session Date: "+str(row[1]))
print("Session Time: "+str(row[2]))
print("Vehicle Regsitration Number: "+str(row[4]))
regNo=row[4]
getPackageFromRegNo(regNo)
return regNo
except:
print("Oops something went wrong 😓")
def getPackageFromRegNo(regNo):
try:
conn = sqlite3.connect('database.db')
cur = conn.cursor()
cur.execute('SELECT * FROM packages WHERE registrationNumber = "{}"'.format(regNo))
rows = cur.fetchall()
for row in rows:
print("Vehicle Type: "+str(row[1]))
print("Tutor Name: "+str(row[3]))
print("Session Duration: "+str(row[4])+"Hours")
print("Price: RM"+str(row[6]))
print("-------------------👌-----------------------")
except:
print("Oops something went wrong 😓")
Here's the output
----------------Sessions------------------
ID: 1
Session ID: 1
----------------Sessions------------------
ID: 2
Session ID: 6
Session Date: 16/03/2022
Session Time: 5:30 PM
Vehicle Regsitration Number: HGT5768
Vehicle Type: Motorbike
Tutor Name: Omar Solieman
Session Duration: 1.0Hours
Price: RM36.78
-------------------👌-----------------------
-----------------------------------
As you can see it only outputs the full data of the last entry
Any help would be appreciated
Thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
