0

based on business requirement, we need to get the data from PostgreSQL document based database in python

PostgreSQL:

select info from "spyConfig" where info @> '{"type" : "processmap"}';

its working as expected in postgresql interface, same query i want to execute in python.

cur.execute("SELECT info FROM spyConfig WHERE info @> %s", 
{'type':'processmap'})

its not working in python, even i want to pass table as parameter, i have tried here and research on it but its not working in python. please give me suggestion to solve this and let me know if any details to add here, i will update as need, Thanks.

5

1 Answer 1

2

The problem is you're not passing your dictionary the right way, you should be passing it like this:

from psycopg2.extras import Json

cur.execute("SELECT info FROM spyConfig WHERE info @> %s", [Json({'type':'processmap'})])

If you want to add the table name also then

from psycopg2.extensions import AsIs
from psycopg2.extras import Json

kwargs = {'table': AsIs('spyConfig'),
          'q': [Json({'type':'processmap'})]}

cur.execute("SELECT info FROM %(table)s WHERE info @> %(q)s", kwargs)
Sign up to request clarification or add additional context in comments.

1 Comment

cur.execute("SELECT info FROM %s WHERE info @> %s", ["spyConfig", Json({'type':'processmap'})]) i am getting below error: psycopg2.ProgrammingError: syntax error at or near "'spyConfig'" in the postgresql data base created a table as "spyConfig" with quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.