请解释这些Python Fetch类型

Ann*_*ary 6 python openerp

这些提取有什么区别.请给我一个参考网站的例子以获得清晰的想法.但是我很困惑

res = cr.dictfetchall()

res2 = cr.dictfetchone()

res3 = cr.fetchall()

res4 = cr.fetchone()
Run Code Online (Sandbox Code Playgroud)

cr是当前行,来自数据库游标(OPENERP 7)

例如:

def _max_reg_no(self, cr, uid, context=None):
    cr.execute("""
    select register_no as reg_no
    from bpl_worker
    where id in (select max(id) from bpl_worker)
    """)
    res = cr.fetchone()[0]
    print (res)
    return res
Run Code Online (Sandbox Code Playgroud)

Sud*_*rya 18

cr.dictfetchall()将以**包含键,值的字典**列表的形式为您提供所有匹配的记录.

cr.dictfetchone()工作方式相同,cr.dictfetchall()只是它只返回单个记录.

cr.fetchall()将以tupple列表的形式为您提供所有匹配的记录.

cr.fetchone()工作方式相同,cr.fetchall()只是它只返回单个记录.

在您的给定查询中,如果您使用:

  1. cr.dictfetchall()会给你的[{'reg_no': 123},{'reg_no': 543},].
  2. cr.dictfetchone()会给你的{'reg_no': 123}.
  3. cr.fetchall() 会给你'[(123),(543)]'.
  4. cr.fetchone() 会给你'(123)'.