title: building a query with kwargs psycopg2
body: <p>I'm trying to dynamically build a query and execute using psycopg2 package. The query which I have looks like below</p>
  
  <pre><code>def get_employees_for_dept(dept_id):
      sql = '''SELECT 
             emp.FirstName,
             emp.LastName,
             dep.DepartmentName
          FROM Employee emp
          JOIN Department dep
          ON
            dp.EmployeeID = emp.Id
          WHERE
            dp.Id = :dept_id''';
  return query.execute(sql, dept_id=dept_id)
  </code></pre>
  
  <p>Now my execute method in query script looks like this:</p>
  
  <pre><code>def execute(sql, **kwargs):
       with closing(psycopg2.connect(dbname=config.instance().db_name,
                                user=config.instance().db_user,
                                password=config.instance().db_password,
                                host=config.instance().db_host,
                                port=config.instance().db_port)) as conn:
         with closing(conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)) as cursor:
         cursor.execute(sql, **kwargs)
         return cursor.fetchall()
  </code></pre>
  
  <p>Now when I run this code  I get an error </p>
  
  <pre><code>cursor.execute(sql, **kwargs)
  TypeError: execute() got an unexpected keyword argument 'user_id'
  </code></pre>
  
  <p>How can I make this work, without doing a string formatting or string builder ?</p>