python - 在 psycopg2 中移动命名光标
问题描述
我在 psycopg2 中使用命名游标。如何重置光标从0开始?我使用了以下代码,但没有效果并且有错误。
with conn.cursor(name="curname") as cursor:
cursor.itersize = 100
cursor.execute("MOVE ABSOLUTE 0 IN curname",)
错误:
LINE 1: DECLARE "curname" CURSOR WITHOUT HOLD FOR MOVE ABSOLUTE 0 IN.
解决方案
您必须使用查询初始化游标cursor.execute()
。然后,您可以使用与客户端游标相同的功能(等)fetchone()
。fetchall()
SQLMOVE
命令由psycopg2中的scroll(value[, mode='relative'])实现。
简单示例(查询生成 10 行,整数从 1 到 10):
with conn.cursor(name="curname") as cursor:
cursor.itersize = 100
cursor.execute("select generate_series(1, 10)")
print('first:', cursor.fetchone())
cursor.scroll(9, mode='absolute')
print('tenth:', cursor.fetchone())
cursor.scroll(0, mode='absolute')
print('first again:', cursor.fetchone())
输出:
first: (1,)
tenth: (10,)
first again: (1,)
阅读有关服务器端游标的更多信息。
推荐阅读
- amazon-web-services - GCP us-central1 区域最接近延迟效率的 AWS 区域是什么?
- snowflake-cloud-data-platform - 将镶木地板文件缓慢加载到雪花
- google-bigquery - 查询 INFORMATION_SCHEMA.JOBS_BY_PROJECT 抛出 dml_stats 错误
- android - 为什么我在尝试在 android studio 中打开新活动时出现错误
- python - 使用当前 RGB 值和亮度计算新的 RGB 值
- sap - Talend - 检索 SAP 表窗口未打开
- swift - Swift ui mac os 小部件滚动列表文本
- javascript - (vue-test-utils) '不能覆盖属性 $route,...'
- java - 在不接触 XML 字符的情况下取消转义 HTML
- ios - iOS:以字节为单位的 UIImage 大小与实际图像大小不同