简体   繁体   English

psycopg2:DictCursor 与 RealDictCursor

[英]psycopg2: DictCursor vs RealDictCursor

AFAIU and from docs, RealDictCursor is a specialized DictCursor that enables to access columns only from keys (aka columns name), whereas DictCursor enables to access data both from keys or index number. AFAIU 和来自 docs, RealDictCursor是一个专门的DictCursor ,它只允许从键(又名列名)访问列,而DictCursor允许从键或索引号访问数据。
I was wondering why RealDictCursor has been implemented if DictCursor offers more flexibility?我想知道如果RealDictCursor提供更大的灵活性,为什么要实施DictCursor Is it performance-wise (or memory-wise) so different (in favor of RealDictCursor I imagine...)?性能方面(或内存方面)是否如此不同(我想支持RealDictCursor ......)?
In other words, what are RealDictCursor use cases vs DictCursor ?换句话说, RealDictCursor用例与DictCursor是什么?

The main advantage of real dictionary cursor is the easiness to get a query output as json.真正的字典游标的主要优点是很容易将查询输出为 json。

Compare:相比:

with psycopg2.connect('dbname=test') as connection:
    with connection.cursor(cursor_factory=RealDictCursor) as cursor:
        cursor.execute("select * from my_table")
        print(json.dumps(cursor.fetchall()))

versus相对

with psycopg2.connect('dbname=test') as connection:
    with connection.cursor() as cursor:
        cursor.execute("select * from my_table")
        columns = [desc[0] for desc in cursor.description]
        real_dict = [dict(zip(columns, row)) for row in cursor.fetchall()]
        print(json.dumps(real_dict))

There is no important difference between these options when it comes to performance.在性能方面,这些选项之间没有重要区别。

You cannot get an expected json using json.dumps(cursor.fetchall()) for regular or dictionary-like cursors and need the conversion showed above.对于常规或类似字典的游标,您无法使用json.dumps(cursor.fetchall())获得预期的 json,并且需要上面显示的转换。 On the other hand, real dictionary cursor produces a much larger result so you should not use it if you really do not need it.另一方面,真正的字典光标会产生更大的结果,因此如果您真的不需要它,则不应使用它。

class psycopg2.extras.类 psycopg2.extras。 RealDictCursor (*args, **kwargs) RealDictCursor (*args, **kwargs)

A cursor that uses a real dict as the base type for rows.使用真正的字典作为行的基本类型的游标。 Note that this cursor is extremely specialized and does not allow the normal access (using integer indices) to fetched data.请注意,此游标非常专业,不允许正常访问(使用整数索引)来获取数据。 If you need to access database rows both as a dictionary and a list, then use the generic DictCursor instead of RealDictCursor .如果您需要以字典和列表的形式访问数据库行,请使用通用DictCursor而不是RealDictCursor class psycopg2.extras.类 psycopg2.extras。 RealDictConnection A connection that uses RealDictCursor automatically. RealDictConnection自动使用RealDictCursor的连接。

Note Not very useful since Psycopg2.5: you can use psycopg2.connect(dsn, cursor_factory= RealDictCursor ) instead of RealDictConnection .注意自 Psycopg2.5 以来不是很有用:您可以使用 psycopg2.connect(dsn, cursor_factory= RealDictCursor ) 而不是RealDictConnection class psycopg2.extras.类 psycopg2.extras。 RealDictRow (cursor) A dict subclass representing a data record. RealDictRow (cursor) 表示数据记录的 dict 子类。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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