1

我正在使用适配器pg8000通过以下代码读取我的数据库中的记录:

cursor = conn.cursor()
results = cursor.execute("SELECT * from data_" + country + " WHERE date >= %s AND date <= %s AND time >= %s AND time <= %s", (datetime.date(fromdate[0], fromdate[1], fromdate[2]), datetime.date(todate[0], todate[1], todate[2]),datetime.time(fromtime[0],fromtime[1]), datetime.time(totime[0],totime[1])))
results = cursor.fetchall()

当我选择一个包含 100 条记录的日期范围时,问题就出现了。它不是大量的记录,但足以导致以下问题,我看不出问题可能来自哪里 - 因为它似乎取决于带回的记录数量。例如:results = cursor.fetchall()似乎工作得很好并返回一个结果。

我得到的错误信息是:

File "/mnt/opt/Centos5.8/python-2.7.8/lib/python2.7/site-packages/pg8000/core.py", line 1650, in handle_messages
    raise error
pg8000.errors.ProgrammingError: ('ERROR', '34000', 'portal "pg8000_portal_0" does not exist')

显然,尽管进行了探索,但我找不到解决此问题的方法。

使用时fetchmany(),结果如下:

results = cursor.fetchmany(100) 作品- 仅限于 100

results = cursor.fetchmany(101) 失败- 与上述相同的错误

4

2 回答 2

7

在自动提交模式下,您检索的行数不能超过 pg8000 缓存所保存的行数(默认为 100)。

我做了一个提交,当这种情况发生时会给出更好的错误消息,这将在 pg8000 的下一个版本中。

原因是,如果查询返回的行数大于 pg8000 缓存中的行数,则数据库门户保持打开状态,然后当缓存为空时,从门户中获取更多行。门户只能存在于事务中,因此在自动提交模式下,在检索到第一批行后,门户会立即关闭。如果您尝试从门户中检索第二批,则会收到问题中报告的“门户不存在”错误。

于 2014-08-18T20:50:35.700 回答
-1

看来这可以通过设置来解决:

conn.autocommit = False

现在代码看起来像:

conn.autocommit = False
cursor = conn.cursor()
results = cursor.execute("SELECT * from data_" + country + " WHERE date >= %s AND date <= %s AND time >= %s AND time <= %s", (datetime.date(fromdate[0], fromdate[1], fromdate[2]), datetime.date(todate[0], todate[1], todate[2]),datetime.time(fromtime[0],fromtime[1]), datetime.time(totime[0],totime[1])))
results = cursor.fetchall()

我不确定为什么会出现这种情况 - 但自动提交设置为 100 的记录数似乎有限制True

于 2014-08-05T09:51:23.237 回答