请解释这些Python提取类型

问题描述:

这些提取有什么区别? 请给我参考网站的例子来获得清晰idea.still我与它请解释这些Python提取类型

res = cr.dictfetchall() 

res2 = cr.dictfetchone() 

res3 = cr.fetchall() 

res4 = cr.fetchone() 

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 
+3

你的示例代码中的“cr”是什么? – 2013-03-28 10:50:51

+0

@olly_uk: 张贴编辑好友 – 2013-03-28 10:54:25

+3

[有关PEP-249中每种方法的完整说明](http://www.python.org/dev/peps/pep-0249/#cursor-methods) – 2013-03-28 10:57:11

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)'。
+0

谢谢arya ..u帮了我很多次:-) – 2013-03-28 11:04:56

+1

@安妮玛丽感谢您的赞美。 – 2013-03-28 11:21:32