I have a PostgreSQL query that won't execute properly when I look for empty strings. I've tried escaping single quotes with the backslash, I've tried double quotes, nothing seems to work. Ideas? I need to look for empty strings because my script doesn't do what I need it to do when it finds empty strings in my dataset.
this_query = """
copy(SELECT phone_number from mytable WHERE phone_number is not null and phone_number != '''' ORDER BY random() LIMIT 25)
to stdout csv header;
"""
Without the empty string phrase (phone_number != ''''), the query will work when I execute:
THIS_COMMAND = "psql -h hostname -d dbname -c '{query}' > {file_name}"
command = THIS_COMMAND.format(query=this_query, file_name=a_file_name)
os.system(command)
ATTN: After reviewing answers below, I experimented and found this to work:
phone_number != '\"'\"''\"'\"'