Lists: | psycopg |
---|
From: | Håvard Wahl Kongsgård <haavard(dot)kongsgaard(at)gmail(dot)com> |
---|---|
To: | psycopg <psycopg(at)postgresql(dot)org> |
Subject: | Iterating through cur and cur.fetchone() |
Date: | 2011-10-10 10:57:12 |
Message-ID: | CAKH9109mY1c=wzdBuV24wMDPXM+94vJzUcPZ404EF6BzgYt_AA@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | psycopg |
Hi, how do I use cur.fetchone() when iterating through a cur object.
with
conn = psycopg2.connect()
cur = conn.cursor()
SQL = ('select * from something limit 1000')
n = 1
cur.execute(SQL)
for edge_list in cur:
edge = cur.fetchone()
print n
n = n +1
I get
n = 500
when I skip edge = cur.fetchone()
for edge_list in cur:
print n
n = n +1
n = 1000
Why does this happen, and what is the solution?
--
Håvard Wahl Kongsgård
From: | Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> |
---|---|
To: | Håvard Wahl Kongsgård <haavard(dot)kongsgaard(at)gmail(dot)com> |
Cc: | psycopg <psycopg(at)postgresql(dot)org> |
Subject: | Re: Iterating through cur and cur.fetchone() |
Date: | 2011-10-10 11:19:14 |
Message-ID: | CA+mi_8aWGEwB-xxQY1nKm-YL3z6BgSZnGo76rgZ5uzybXSW4oQ@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | psycopg |
2011/10/10 Håvard Wahl Kongsgård <haavard(dot)kongsgaard(at)gmail(dot)com>:
> Hi, how do I use cur.fetchone() when iterating through a cur object.
>
> with
> conn = psycopg2.connect()
> cur = conn.cursor()
> SQL = ('select * from something limit 1000')
>
> n = 1
> cur.execute(SQL)
> for edge_list in cur:
> edge = cur.fetchone()
> print n
>
> n = n +1
>
> I get
> n = 500
>
> when I skip edge = cur.fetchone()
>
> for edge_list in cur:
> print n
>
> n = n +1
>
> n = 1000
>
> Why does this happen, and what is the solution?
You are consuming the cursor both with the iteration and with the
fetchone. You should either use:
for record in cur:
# use record
or
while 1:
record = cur.fetchone()
if not record: break
# use record
Not mix the two together.
-- Daniele
From: | Håvard Wahl Kongsgård <haavard(dot)kongsgaard(at)gmail(dot)com> |
---|---|
To: | psycopg <psycopg(at)postgresql(dot)org> |
Subject: | Re: Iterating through cur and cur.fetchone() |
Date: | 2011-10-10 11:19:23 |
Message-ID: | CAKH910-YZ_XZLAcbwSd=T5907dXbJHf9XoUGAZYEUxbR4h2w-Q@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | psycopg |
Sorry, I stupid mistake. But I have been using psycopg for a year and
I have some issue with memory usage on large cursors.
Any tips on how to reduce the memory usage.
-Håvard
2011/10/10 Håvard Wahl Kongsgård <haavard(dot)kongsgaard(at)gmail(dot)com>:
> Hi, how do I use cur.fetchone() when iterating through a cur object.
>
> with
> conn = psycopg2.connect()
> cur = conn.cursor()
> SQL = ('select * from something limit 1000')
>
> n = 1
> cur.execute(SQL)
> for edge_list in cur:
> edge = cur.fetchone()
> print n
>
> n = n +1
>
> I get
> n = 500
>
> when I skip edge = cur.fetchone()
>
> for edge_list in cur:
> print n
>
> n = n +1
>
> n = 1000
>
> Why does this happen, and what is the solution?
>
>
> --
> Håvard Wahl Kongsgård
>
From: | Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> |
---|---|
To: | Håvard Wahl Kongsgård <haavard(dot)kongsgaard(at)gmail(dot)com> |
Cc: | psycopg <psycopg(at)postgresql(dot)org> |
Subject: | Re: Iterating through cur and cur.fetchone() |
Date: | 2011-10-10 11:26:14 |
Message-ID: | CA+mi_8ZA3b3+NbcW6uonovkJuCPn-4Kn1TNLJYEXCZ56yX6U2A@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | psycopg |
2011/10/10 Håvard Wahl Kongsgård <haavard(dot)kongsgaard(at)gmail(dot)com>:
> Sorry, I stupid mistake. But I have been using psycopg for a year and
> I have some issue with memory usage on large cursors.
> Any tips on how to reduce the memory usage.
You can use server-side cursors, aka named cursors
<http://initd.org/psycopg/docs/usage.html#server-side-cursors>. In the
latest psycopg versions, iteration on the cursor (i.e. the "for record
in cur: ..." syntax) is more efficient than iterating with fetchone.
Everything is explained in the docs.
-- Daniele