Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- # Define find_hotels()
- def find_hotels(params):
- # Create the base query
- query = 'SELECT * FROM hotels'
- # Add filter clauses for each of the parameters
- if len(params) > 0:
- filters = ["{}=?".format(k) for k in params]
- query += " WHERE " + " and ".join(filters)
- # Create the tuple of values
- t = tuple(params.values())
- # Open connection to DB
- conn = sqlite3.connect("hotels.db")
- # Create a cursor
- c = conn.cursor()
- # Execute the query
- c.execute(query,t)
- # Return the results
- print(c.fetchall())
- # Create the dictionary of column names and values
- params = {"area": "south" , "price": "lo"}
- # Find the hotels that match the parameters
- print(find_hotels(params))
Add Comment
Please, Sign In to add comment