简体   繁体   中英

psycopg2.ProgrammingError: no results to fetch

I'm new to PostgreSQL.

My code uses Postgres through psycopg2:

try:
    statement = "select wie_scan_id, job_status from jobs where job_id = %s"
    scan = pg_db.get(statement, (job_id, ))
    scan_id = scan[0]["wie_scan_id"]
    
    
except Exception as e:
    logging.error("Error in resuming scan: "+ repr(e))
    set_error_on_db(job_id, "Error in resuming scan")
    return

where pg_db.get is:

    def get(self, statement, data=None, job_id=None):
        logging.debug("Starting get query thread for job id %s", job_id)
        if data:
            self._cursor.execute(statement, data)
        else: 
            self._cursor.execute(statement)

        if "delete from" in statement.lower():
            return

        result = []
        columns = tuple([d[0].decode('utf8') for d in self._cursor.description])
        for row in self._cursor:
            result.append(dict(zip(columns, row)))
        return result

and I'm getting this exception:

ERROR, 234, Error in resuming scan: ProgrammingError('no results to fetch',)

is there any mistake I make in making the query?

You are iterating through a result set of 0, so you get the error. Add a row count check to your code and that should prevent this, eg

if self._cursor.rowcount != 0:
    for row in self._cursor:
        result.append(dict(zip(columns, row)))
    return result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM