Search code examples
pythonpython-2.7postgresqlpygresql

Python - convert pgqueryobject to string


Prelude:
Python version: 2.7.9
Pygresql version: 4.0

Problem:
I am trying to check if a sub string exists within a pgqueryobject using python.

Basically I have this in terms of code:

import pg
con = pg.connect(...)
q = con.query(...)

Where q is a pgqueryobject returned from the con.query(...)

Calling print(q) prints the entire table however converting q to a string such as:

string = str(q)
print(string)

Prints "<pg query result>".

I am trying to see if table has now instances (rows) which I planned on doing by converting the table to a string and searching for "0 rows" but I can't seem to figure out how to convert it to a string.

Is there I way I can go about doing this?


Solution

  • You can most likely accomplish this by saving the output of q.getresult() into a variable and then casting it into a string.

    http://www.pygresql.org/contents/pg/query.html#pg.Query.getresult