Please explain these Python Fetch types

This is the place for queries that don't fit in any of the other categories.

Please explain these Python Fetch types

Postby priyanka_hdp » Thu Mar 28, 2013 10:37 am

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
Code: Select all
    res = cr.dictfetchall()

    res2 = cr.dictfetchone()

    res3 = cr.fetchall()

    res4 = cr.fetchone()

cr used in openerp 7

Code: Select all
    def _max_reg_no(self, cr, uid, context=None):
        select register_no as reg_no
        from bpl_worker
        where id in (select max(id) from bpl_worker)
        res = cr.fetchone()[0]
        return res
Last edited by priyanka_hdp on Thu Mar 28, 2013 10:47 am, edited 2 times in total.
Posts: 5
Joined: Thu Mar 28, 2013 10:35 am

Re: Please explain these Python Fetch types

Postby micseydel » Thu Mar 28, 2013 10:40 am

What is cr?
Join the #python-forum IRC channel on for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from :)
User avatar
Posts: 2904
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Please explain these Python Fetch types

Postby setrofim » Thu Mar 28, 2013 11:07 am

It looks like cr is some kind of a query object from an ORM. If that is the case, then the difference is that fetchall() will return a list of all the results (even if there is one result, it will be returned as a list of length one). fetchone() will return the one result (not in a list); but will raise an exception if there isn't exactly one result. By default, the result will be either a list or a tuple of values. The dict- versions will return the result as a dict mapping column names to the values.

But yeah, as micseydel said, we'd need to know what cr is to be able to tell for sure.
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests