I'm working with the following code in python, calling a PostgreSQL query with subprocess:
import subprocess
claimer_name = 'a_name'
startdate = '2014-04-01'
enddate = '2018-04-01'
data = subprocess.check_output(['/usr/bin/psql -U user_name "SELECT c.asset_id, c.video_id,
c.claim_id, c.claim_date FROM db.claim c JOIN db.claim_history h ON c.claim_id = h.claim_id JOIN
db.users_email e ON LOWER(e.email) = LOWER(h.email) JOIN m.auth_user u ON e.user_id = u.id WHERE
h.list_order = 1 AND c.claim_origin = ‘Descriptive Search’ AND c.claim_date >= \"%s\" AND
c.claim_date < \"%s\" AND concat(u.first_name, concat(chr(32),
u.last_name)) = \"%s\""' % (startdate, enddate, claimer_name)], shell=True)
How can I escape the single quotes around 'Descriptive Search'? Running this code as-is gives the error Only ASCII characters are allowed in an identifier.
I have tried:
[''Descriptive Search''][\'Descriptive Search\'][""Descriptive Search""][concat('Descriptive', concat(chr(32), 'Search'))]
and assigning a variable: i = 'Descriptive Search', and then c.claim_origin = \"%s\".
However, these attempts yield the same ASCII characters error. Using string-formatting works fine for my other variables (startdate, enddate, claimer_name) and I'm stumped as to why it doesn't work for the string 'Descriptive Search'.
Using PostgreSQL 9.3.
Any help or points in the right direction would be great; thanks!