![](/img/trans.png)
[英]Python Postgres - psycopg2.ProgrammingError: no results to fetch
[英]psycopg2.ProgrammingError: no results to fetch
我是 PostgreSQL 的新手。
我的代码通过 psycopg2 使用 Postgres:
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
其中pg_db.get
是:
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
我得到了这个例外:
ERROR, 234, Error in resuming scan: ProgrammingError('no results to fetch',)
我在查询时有什么错误吗?
您正在遍历结果集 0,因此您收到错误。 在您的代码中添加行计数检查,这应该可以防止这种情况,例如
if self._cursor.rowcount != 0:
for row in self._cursor:
result.append(dict(zip(columns, row)))
return result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.