Re: Query ID Values

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org, Ian Zimmerman <itz(at)very(dot)loosely(dot)org>
Subject: Re: Query ID Values
Date: 2018-05-15 12:53:10
Message-ID: dcd91fab-9b28-6c77-ebc5-0dd604da52b1@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 05/14/2018 10:04 PM, Ian Zimmerman wrote:
> On 2018-05-14 21:12, Adrian Klaver wrote:
>
>> Because you are doing fetchall(). That is going to fetch a list of row
>> tuples. Either iterate over that list or iterate over the cursor:
>>
>> for row in cur_p:
>> print(row)
>>
>> For more info see:
>> http://initd.org/psycopg/docs/cursor.html
>
> Where does that webpage say that I can use the cursor itself for
> iteration? I can't find it there. (OTOH it is clearly documented for
> the sqlite3 library).

Results retrieval methods
"
Note

cursor objects are iterable, so, instead of calling explicitly
fetchone() in a loop, the object itself can be used:

>>> cur.execute("SELECT * FROM test;")
>>> for record in cur:
... print record

"

>
> Until now, with psycopg2 I have done it like this:
>
> cur.execute(stmt)
> results = iter(cur.fetchone, None)
> for r in results:
> ...
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Pavel Stehule 2018-05-15 12:55:33 Re: Function to set up variable inside it
Previous Message John McKown 2018-05-15 12:29:24 Re: Control PhoneNumber Via SQL