Guest User

Untitled

a guest
Aug 10th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. import sqlite3
  2.  
  3. # Define find_hotels()
  4. def find_hotels(params):
  5. # Create the base query
  6. query = 'SELECT * FROM hotels'
  7. # Add filter clauses for each of the parameters
  8. if len(params) > 0:
  9. filters = ["{}=?".format(k) for k in params]
  10. query += " WHERE " + " and ".join(filters)
  11. # Create the tuple of values
  12. t = tuple(params.values())
  13.  
  14. # Open connection to DB
  15. conn = sqlite3.connect("hotels.db")
  16. # Create a cursor
  17. c = conn.cursor()
  18. # Execute the query
  19. c.execute(query,t)
  20. # Return the results
  21. print(c.fetchall())
  22.  
  23.  
  24. # Create the dictionary of column names and values
  25. params = {"area": "south" , "price": "lo"}
  26.  
  27. # Find the hotels that match the parameters
  28. print(find_hotels(params))
Add Comment
Please, Sign In to add comment