python中游标具体使用方式
简单总结游标or流式读取的这块使用方式,具体使用方式还是得看官方文档。游标使用场景适合于超大规模的数据库数据读取。具体可以看我上篇文章:从数据库读取大量数据的方式对比
psycopg2
适用对象:mysql数据库
postgresql提供了数据库游标的方式,可以不用一次性读到程序内存里,而是分多次加载到内存里,按批处理。
cursor
( name=None , cursor_factory=None , scrollable=None , withhold=False )Return a new
cursor
object using the connection.If name is specified, the returned cursor will be a server side cursor (also known as named cursor ). Otherwise it will be a regular client side cursor. By default a named cursor is declared without
SCROLL
option andWITHOUT HOLD
: set the argument or propertyscrollable
toTrue
/False
and orwithhold
toTrue
to change the declaration.
使用操作:
- 指定cursor名字:psycopg2_conn.cursor(name=‘name_of_cursor’) (划重点,需要赋予名字才会开启数据库的游标,不指定na