CHallenge code: as50 (50 points)
Adding records
def add_car():
with conn:
Creates a function and opens the database up
NewCarName = input("Car Name: ")
NewCarSpeed = input("Top Speed of car: ")
Two inputs to add the data to the field.
conn.execute("INSERT INTO Cars(Name,TopSpeed) VALUES(?,?)", (NewCarName,NewCarSpeed))
One long line, this may break across 2 in some browsers.
Broken down this is:
INSERT INTO Cars(Name,TopSpeed)
This part of the line says where we are going to put the data into.
VALUES(?,?)
These are placeholders for the two variables.
(NewCarName,NewCarSpeed)
The variable names
Hint: Because we've added an autonumber, this will take care of itself. That does NOT mean it will fill in gaps.
with conn:
Creates a function and opens the database up
NewCarName = input("Car Name: ")
NewCarSpeed = input("Top Speed of car: ")
Two inputs to add the data to the field.
conn.execute("INSERT INTO Cars(Name,TopSpeed) VALUES(?,?)", (NewCarName,NewCarSpeed))
One long line, this may break across 2 in some browsers.
Broken down this is:
INSERT INTO Cars(Name,TopSpeed)
This part of the line says where we are going to put the data into.
VALUES(?,?)
These are placeholders for the two variables.
(NewCarName,NewCarSpeed)
The variable names
Hint: Because we've added an autonumber, this will take care of itself. That does NOT mean it will fill in gaps.
Deleting a record
def del_car():
with conn:
Creates a function and opens the database up
DeleteCar = input("Car ID for deletion:")
Asks for the ID you'd like to delete.
conn.execute("DELETE FROM Cars WHERE id = ?", (DeleteCar,))
Deletes it!
with conn:
Creates a function and opens the database up
DeleteCar = input("Car ID for deletion:")
Asks for the ID you'd like to delete.
conn.execute("DELETE FROM Cars WHERE id = ?", (DeleteCar,))
Deletes it!
Bonus: IDIOT PROOFING
For 50 bonus points, can you create a routine that looks up the record you are going to delete and asks for user input to confirm that you want to delete it before you do it?