title: psycopg2 escaping characters
body: <p>I have a postgres query that looks like the following, and runs find on psql:</p>
  
  <pre><code>select 
      gcaseid,
      count (*)                          as N,
      array_agg(distinct nvchquestion)   as questions,
      array_agg(nvchanswer)              as answers
  from
      case_script,
      case_script_answer
  where
      case_script.gscriptid = case_script_answer.gscriptid and
      nvchquestion ilike '%blood pressure%'
  group by
      gcaseid
  order by
      N desc
  ;
  </code></pre>
  
  <p>Now, I wanted to have a similar query in Python, And this is what I came up with:</p>
  
  <pre><code>import psycopg2
  
  def getAnswers(question):
  
      query = '''
              select 
                  gcaseid,
                  count (*)                          as N,
                  array_agg(distinct nvchquestion)   as questions,
                  array_agg(nvchanswer)              as answers
              from
                  case_script,
                  case_script_answer
              where
                  case_script.gscriptid = case_script_answer.gscriptid and
                  nvchquestion ilike %s
              group by
                  gcaseid
              order by
                  N desc
              ;
      '''%(r"'%%s%'")
  
      conn = psycopg2.connect("dbname='sos' user='postgres' host='localhost'")
      cur  = conn.cursor()
  
      cur.mogrify(query, (question,))
      # cur.execute(query, (question,))
  
      # result = cur.fetchall()
  
      cur.close()
      conn.close()
  
      return result
  
  if __name__ == '__main__':
  
      print getAnswers('blood pressure')
      print 'done'
  </code></pre>
  
  <p>Now, when I ran this query, I got the error:</p>
  
  <pre><code>$ python updateTable.py
  Traceback (most recent call last):
    File "updateTable.py", line 39, in &lt;module&gt;
      print getAnswers('blood pressure')
    File "updateTable.py", line 27, in getAnswers
      cur.mogrify(query, (question,))
  ValueError: unsupported format character ''' (0x27) at index 442
  </code></pre>
  
  <p>Not sure what is happening. Anyone can clarify please?</p>