import sqlite3
loop = True
def purchase_info():
conn = sqlite3.connect('car.db')
cursor = conn.cursor()
sql = '''SELECT clients.First_name, clients.Surname, cars.Make, cars.Model, purchase.Date_of_Purchase FROM
((purchase INNER JOIN cars ON purchase.numbID = cars.numbID) INNER JOIN clients ON purchase.ClientID = clients.ClientID)
WHERE purchaseID = ?''';
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print ("First_name: " + str(row[0]) +", Surname: " +
str(row[1]) + ", Make: " + str(row[2]) + ",Model: " +
str(row[3]) + ",Date_of_Purchase: " +str(row[4]))
def displaymenu():
print("1 to display records")
print("2 to enter records")
print("3 to update records")
print("4 to delete records")
print("5 to exit the menu\n")
#print("")
def select():
conn = sqlite3.connect('car.db')
cursor = conn.cursor()
sql = 'SELECT * FROM cars';
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print ("numbID: " + str(row[0]) +", Make: " +
str(row[1]) + ", Model: " + str(row[2]) + ",Colour: " +
str(row[3]) + ",Year: " +str(row[4]) + ",Transmission:" +
str(row[5]) + ",Price: "+str(row[6]) + ",Number In Stock:" +str(row[7]))
def insert():
var_choice = int(input("Select the number of cars to input "))
var_count = 1
while var_choice >=var_count:
var_Make = str(input("Input car make. "))
var_Model = str(input("Input car model. "))
var_Colour = str(input("Input car's colour. "))
var_Year = str(input("Input car release date."))
var_Transmission= str(input("Input car's transmission type."))
var_Price=str(input("Input car's price."))
var_Stock= str(input("Input car stock number."))
var_insert = []
var_insert.append(var_Make)
var_insert.append(var_Model)
var_insert.append(var_Colour)
var_insert.append(var_Year)
var_insert.append(var_Transmission)
var_insert.append(var_Price)
var_insert.append(var_Stock)
conn = sqlite3.connect('car.db')
cursor = conn.cursor()
print(var_insert)
cursor.execute('insert INTO cars(Make, Model, Colour, Year,Transmission,Price,Stock)VALUES (?,?,?,?,?,?,?);', var_insert)
conn.commit()
var_count += 1
def update():
var_updt = str(input("Give the ID number of the car to update. "))
var_updt_Make = str(input("Give the new make of car. "))
var_updt_Model= str(input("Give the new model of car."))
var_updt_Colour= str(input("Give the new colour of the car."))
var_updt_Year= str(input("Give the new year of the car."))
var_updt_Transmission= str(input("Give the new transmission of the car."))
var_updt_Price= str(input("Give the new pricing of the car."))
var_updt_Stock = str(input("How many cars in stock?"))
var_update = []
var_update.append(var_updt_Make)
var_update.append(var_updt_Model)
var_update.append(var_updt_Colour)
var_update.append(var_updt_Year)
var_update.append(var_updt_Transmission)
var_update.append(var_updt_Price)
var_update.append(var_updt_Stock)
var_update.append(var_updt)
conn = sqlite3.connect('car.db')
cursor = conn.cursor()
print(var_update)
cursor.execute('UPDATE cars SET Make = ?, Model= ?, Colour=?, Year=?, Transmission=?, Price=?, Stock=? WHERE numbID =?', var_update)
conn.commit()
def delete():
var_del =[]
var_del_choice = str(input("Give the ID number of the car record to delete."))
var_del.append(var_del_choice)
#print (var_del)
conn = sqlite3.connect ('car.db')
cursor = conn.cursor()
cursor.execute ('DELETE FROM car WHERE numbID =?', var_del)
conn.commit()
while loop:
displaymenu()
var_choice = input("Please make your choice using numbers 1-5.")
print("")
if var_choice =='1':
select()
elif var_choice =='2':
insert()
elif var_choice =='3':
update()
elif var_choice =='4':
delete()
elif var_choice =='5':
#this next statment turns the loop off
loop= False
elif var_choice =="":
# the double quotes together are a null value
print("Please enter a number.")
else:
print("Invalid input.")
What I have tried:
This code has crud system but I want to create an inner join table with tables clients, cars and purchase table. There are foreign keys included but I'm not sure how it works. Please help me.