title: "ProgrammingError: syntax error at or near" when executing query in python using psycopg2
body: <p>I am running Python v 2.7 and psycopg2 v 2.5
  I have a postgresql database function that returns a sql query as a text field.</p>
  
  <p>I am using the following code to call the function and extract the query from the text field.</p>
  
  <pre><code>cur2.execute("SELECT * FROM historian.generate_get_archive_data_sql(%s, '*',public.get_dcs_datetime_from_timestamp(%s),public.get_dcs_datetime_from_timestamp(%s))",(row,old_time,current_time))
  while True: #Indefinite Loop
      row2 = cur2.fetchone()
      query2 = str(row2)
      cur3.execute(query2)
  </code></pre>
  
  <p>I seem to be getting the following error when running the script,</p>
  
  <blockquote>
    <p>ProgrammingError: syntax error at or near "'SELECT * FROM
    historian._73b4f984f30b4c68a871fac1c53a1b8f_45907294814208000 WHERE
    archive_datetime BETWEEN 45907381384722560 AND 45907381384732560 ORDER
    BY archive_datetime;'" LINE 1: ('SELECT * FROM
    historian.<em>73b4f984f30b4c68a871fac1c53a1b8f</em>...</p>
  </blockquote>
  
  <p>When i run the query on the postgresql server it executes fine. Can someone help me understand what i am doing wrong ? . Thanks.</p>