Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain these Python Fetch types

Tags:

python

openerp

What is the difference between these fetching.? please give me a examples for reference site to get clear idea.still i'm confuse with it

res = cr.dictfetchall()

res2 = cr.dictfetchone()

res3 = cr.fetchall()

res4 = cr.fetchone()

cr is the current row, from the database cursor (OPENERP 7 )

ex :

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
like image 281
Anne Mary Avatar asked Mar 28 '13 10:03

Anne Mary


1 Answers

cr.dictfetchall() will give you all the matching records in the form of ** list of dictionary** containing key, value.

cr.dictfetchone() works same way as cr.dictfetchall() except it returns only single record.

cr.fetchall() will give you all the matching records in the form of list of tupple.

cr.fetchone() works same way as cr.fetchall() except it returns only single record.

In your given query, if you use:

  1. cr.dictfetchall() will give you [{'reg_no': 123},{'reg_no': 543},].
  2. cr.dictfetchone() will give you {'reg_no': 123}.
  3. cr.fetchall() will give you '[(123),(543)]'.
  4. cr.fetchone() will give you '(123)'.
like image 134
Sudhir Arya Avatar answered Nov 09 '22 18:11

Sudhir Arya