473,408 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Cursor navigation

TA
Hi,

This might be a silly question, but I was wondering how you would navigate
backwards in a PostgreSQL cursor when the Python DB-API 2.0 allows records
to be fetched in a forward-only manner? I have tried different solutions -
two of which are included here.

The first solution uses the cursor declared implicitly by the pyPgSQL
interface, but for some reason this does not work as expected.

(RH-8.0, Python-2.2.1, pyPgSQL-2.4.0, PostgreSQL-7.2.2)
# First solution
from pyPgSQL import PgSQL
con = PgSQL.connect()
con.conn.toggleShowQuery 'On' cur = con.cursor() QUERY: BEGIN WORK cur.execute("select * from test") QUERY: DECLARE "PgSQL_081B3B64" CURSOR FOR select * from test
QUERY: FETCH 1 FROM "PgSQL_081B3B64"
QUERY: SELECT typname, -1 , typelem FROM pg_type WHERE oid = 23
QUERY: SELECT typname, -1 , typelem FROM pg_type WHERE oid = 1043 cur.execute("fetch 1 in \"%s\" " % cur.name) QUERY: fetch 1 in "PgSQL_081B3B64" r = cur.fetchall() QUERY: FETCH ALL FROM "PgSQL_081B3B64" r [[2, 'name2'], [3, 'name3'], [4, 'name4'], [5, 'name5'], [6, 'name6']]

The 'fetchall()' returns the remaining five rows in the table and not just a
single row as expected.
The other solution explicitly declares another cursor.
# Second solution
from pyPgSQL import PgSQL
con = PgSQL.connect()
con.conn.toggleShowQuery 'On' cur = con.cursor() QUERY: BEGIN WORK cur.execute("declare acursor cursor for select * from test") QUERY: declare acursor cursor for select * from test cur.execute("fetch 1 in acursor") QUERY: fetch 1 in acursor
QUERY: SELECT typname, -1 , typelem FROM pg_type WHERE oid = 23
QUERY: SELECT typname, -1 , typelem FROM pg_type WHERE oid = 1043 r = cur.fetchall()
r [[1, 'name1']] cur.execute("fetch 1 in acursor") QUERY: fetch 1 in acursor r = cur.fetchall()
r [[2, 'name2']] cur.execute("fetch -1 in acursor") QUERY: fetch -1 in acursor r = cur.fetchall()
r

[[1, 'name1']]

This works as expected. Only a single row is returned and it is possible to
fetch rows forwards and backwards in the cursor, but is this really the way
to do this?

If anyone can explain what I'm doing wrong or suggest another approach, it
would be much appreciated.

--
TA
Denmark
Jul 21 '05 #1
2 1978
TA wrote:
Hi,

This might be a silly question, but I was wondering how you would navigate
backwards in a PostgreSQL cursor when the Python DB-API 2.0 allows records
to be fetched in a forward-only manner?


This is untrue: cursor.scroll() is an optional DB-API 2.0 extension.

http://www.python.org/peps/pep-0249.html

MySQLdb supports this, but I do not know about your case.

Jul 21 '05 #2
TA

"Andy Dustman" <fa*******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

This is untrue: cursor.scroll() is an optional DB-API 2.0 extension.

http://www.python.org/peps/pep-0249.html

MySQLdb supports this, but I do not know about your case.

Yes 'cursor.scroll()' is optional and pySqlite supports it too, but pyPgSQL
does not.

Thanks for replying.

--
TA
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: S.W. Rasmussen | last post by:
A trivial (?) question: does anyone know how to change the shape of the cursor in a RichTextBox control from the normal vertical line to an underscore?
9
by: Chris Michael | last post by:
I am using the following class in a CSS document: ..loginsubmit { background-color: #ffffff; FONT-WEIGHT: bold; color: #002980; cursor: hand } It works perfectly well, but it won't validate...
10
by: Bill H | last post by:
I used a bit of JS on a page that is fairly long and didn't like the way that <A HREF='#' ONCLICK=\"... refreshed the page to the top. Removing the HREF (<A ONCLICK=\"...) solved my...
0
by: Veli-Pekka Tätilä | last post by:
Hi, My first post here. I've found some serious accessibility flaws in the Python 2.4 docs and wish they could be rectified over time. I'm very new to Python and initially contacted docs at python...
4
by: PJ6 | last post by:
This is the only solution I've found to get a wait cursor document-wide: function Busy() { if (document.all) for (var i=0;i < document.all.length; i++) document.all(i).style.cursor = 'wait'; }...
4
by: david.isaacks | last post by:
Is there a way on a form of making one field the default field such that as soon as you navigate to the form page, the cursor is sitting on that field ready for data to be entered.
0
by: Phil Stanton | last post by:
I have a form with 3 Tab Controls, one of which has a continuous subform with an image in the footer. If on the main form, I move through the records using the navigation buttons, the Cursor...
10
by: EA | last post by:
I am sure I must be missing something about building navigation bars with CSS. Yes it is a very clever and efficient way to format navigation structures on simple one navigation level webs, i.e....
1
by: Phil Stanton | last post by:
Is there any way of stopping the cursor jumping to a point roughly in the middle of the screen when loading a new picture into an Image Control. The picture is linked and pulled from an external...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.