Challenge CODE: AS49 (50 Points)
The Setup
This isn't going to be much of a database unless we can search for things!
def search_by_speed():
MinSpeed = input("I want a car faster than: ")
with conn:
This includes the function, a variable to input and the line to open up the Database.
def search_by_speed():
MinSpeed = input("I want a car faster than: ")
with conn:
This includes the function, a variable to input and the line to open up the Database.
The select command
cur = conn.execute("SELECT TopSpeed, Name FROM Cars WHERE TopSpeed>? ORDER BY TopSpeed", (MinSpeed,))
This line is necessarily long (and may appear as 2 lines in your browser)
SELECT TopSpeed, Name FROM Cars
This says the fields we'd like from the table Cars
WHERE TopSpeed>?
This chooses from the records where it finds that TopSpeed is greater than the variable we just put in. (Remember ? is a placeholder)
ORDER BY TopSpeed
We can choose which order this will appear in. By default it is ascending. e.g. 0-9, A-Z, but if we'd like to go the other way add: DESC
(MinSpeed,)
This is the variable we want to put in place of the ? Don't forget the comma!
This line is necessarily long (and may appear as 2 lines in your browser)
SELECT TopSpeed, Name FROM Cars
This says the fields we'd like from the table Cars
WHERE TopSpeed>?
This chooses from the records where it finds that TopSpeed is greater than the variable we just put in. (Remember ? is a placeholder)
ORDER BY TopSpeed
We can choose which order this will appear in. By default it is ascending. e.g. 0-9, A-Z, but if we'd like to go the other way add: DESC
(MinSpeed,)
This is the variable we want to put in place of the ? Don't forget the comma!
Descending order
cur = conn.execute("SELECT TopSpeed, Name FROM Cars WHERE TopSpeed>? ORDER BY TopSpeed DESC", (MinSpeed,))
Nearly the same as the one above, but with the DESC keyword added.
Print the rows
for row in cur:
print(row[0],"mph ",row[1])
Finally finish off with a for loop and a list of cars.
print(row[0],"mph ",row[1])
Finally finish off with a for loop and a list of cars.