Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. from psycopg2.extensions import adapt
  2.  
  3.  
  4. def get_query(query, params=None):
  5. """Returns the query which is supposed to be run in Postgres.
  6.  
  7. :param query: String representation of the query
  8. :param params: Dict of parameters
  9. :return: String representation of the query when the parameters are filled out
  10. """
  11. actual_params = {k: adapt(v) for k, v in params.iteritems() if params}
  12. _query = query % actual_params
  13. return _query
  14.  
  15. query_to_run = 'SELECT * FROM accounts WHERE id = %(id)s'
  16.  
  17. params = {'id': '1'}
  18.  
  19. print(get_query(query_to_run, params))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement