diff options
author | Federico Di Gregorio <fog@initd.org> | 2005-10-22 06:59:31 +0000 |
---|---|---|
committer | Federico Di Gregorio <fog@initd.org> | 2005-10-22 06:59:31 +0000 |
commit | ef3430d24f3a1580a994e6dec8b5926a752efaed (patch) | |
tree | baa59cd9e3208147bc476eddf743088860287227 /examples/fetch.py | |
parent | f687f2853ed619e4ee1b53f350cc31158e81e9cf (diff) | |
download | psycopg2-ef3430d24f3a1580a994e6dec8b5926a752efaed.tar.gz |
Implemented named cursors.
Diffstat (limited to 'examples/fetch.py')
-rw-r--r-- | examples/fetch.py | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/examples/fetch.py b/examples/fetch.py index e80859b..67818d2 100644 --- a/examples/fetch.py +++ b/examples/fetch.py @@ -52,19 +52,28 @@ conn.commit() # does some nice tricks with the transaction and postgres cursors # (remember to always commit or rollback before a DECLARE) - -curs.execute("DECLARE crs CURSOR FOR SELECT * FROM test_fetch") -curs.execute("FETCH 10 FROM crs") -print "First 10 rows:", flatten(curs.fetchall()) -curs.execute("MOVE -5 FROM crs") +# +# we don't need to DECLARE ourselves, psycopg now support named +# cursor (but we leave the code here, comments, as an example of +# what psycopg is doing under the hood) +#curs.execute("DECLARE crs CURSOR FOR SELECT * FROM test_fetch") +#curs.execute("FETCH 10 FROM crs") +#print "First 10 rows:", flatten(curs.fetchall()) +#curs.execute("MOVE -5 FROM crs") +#print "Moved back cursor by 5 rows (to row 5.)" +#curs.execute("FETCH 10 FROM crs") +#print "Another 10 rows:", flatten(curs.fetchall()) +#curs.execute("FETCH 10 FROM crs") +#print "The remaining rows:", flatten(curs.fetchall()) + +ncurs = conn.cursor("crs") +ncurs.execute("SELECT * FROM test_fetch") +print "First 10 rows:", flatten(ncurs.fetchmany(10)) +ncurs.scroll(-5) print "Moved back cursor by 5 rows (to row 5.)" -curs.execute("FETCH 10 FROM crs") -print "Another 10 rows:", flatten(curs.fetchall()) -curs.execute("FETCH 10 FROM crs") -print "The remaining rows:", flatten(curs.fetchall()) - -# rollback to close the transaction - +print "Another 10 rows:", flatten(ncurs.fetchmany(10)) +print "Another one:", list(ncurs.fetchone()) +print "The remaining rows:", flatten(ncurs.fetchall()) conn.rollback() curs.execute("DROP TABLE test_fetch") |