CHallenge code: AS51
create function & open DB
def update_a_car():
with conn:
This defines the function and opens the DB.
with conn:
This defines the function and opens the DB.
choose a record and print it
update_car = input("Car id to update:")
This enables you to select a record
cur = conn.execute("SELECT id, Name, TopSpeed FROM Cars WHERE id = ?", (update_car,))
This line selects the record for printing
row = cur.fetchone()
This line fetches the record from the DB.
print ("ID:",row[0],"Name:",row[1],"Top Speed:",row[2],"mph")
This prints it.
This enables you to select a record
cur = conn.execute("SELECT id, Name, TopSpeed FROM Cars WHERE id = ?", (update_car,))
This line selects the record for printing
row = cur.fetchone()
This line fetches the record from the DB.
print ("ID:",row[0],"Name:",row[1],"Top Speed:",row[2],"mph")
This prints it.
Updating the record
car_top_speed = input("The new top speed is:")
This inputs the new top speed
conn.execute("UPDATE Cars SET TopSpeed=? WHERE id=?", (car_top_speed, update_car))
UPDATE - Says you are going to change a record
SET TopSpeed=? - Says which field and the new value
WHERE id=? - Chooses the id field and matches it.
Hint: Be careful this command can change many records at once. You're a programmer now and the safeties are off!
This inputs the new top speed
conn.execute("UPDATE Cars SET TopSpeed=? WHERE id=?", (car_top_speed, update_car))
UPDATE - Says you are going to change a record
SET TopSpeed=? - Says which field and the new value
WHERE id=? - Chooses the id field and matches it.
Hint: Be careful this command can change many records at once. You're a programmer now and the safeties are off!
Bonus - Let people know the new value
For a simple 10 bonus points, let people know the new value of the record.