Christopher Tyler

Escape Keys using psycopg2

Found the information at https://www.psycopg.org/docs/usage.html#query-parameters.

Since % is used in both Python and in Postgres I need to use %% instead:

1
2
>>> cur.execute("SELECT (%s % 2) = 0 AS even", (10,))       # WRONG
>>> cur.execute("SELECT (%s %% 2) = 0 AS even", (10,))      # correct

or this:

1
2
3
4
5
6
f""" AND '{findings}' %%> ANY(STRING_TO_ARRAY(
      CONCAT("Finding1","Finding2","Finding3","Finding4","Finding5","Finding6")
      ,' '
      )
     )
"""

instead of this:

1
2
3
4
5
6
f""" AND '{findings}' %> ANY(STRING_TO_ARRAY(
      CONCAT("Finding1","Finding2","Finding3","Finding4","Finding5","Finding6")
      ,' '
      )
     )
"""

<< Previous Post

|

Next Post >>

#Til #Psycopg2 #Python #Postgresql