Walking through a mysql db

J

Jeff Elkins

I'm writing a small wxpython app to display and update a dataset. So far, I
get the first record for display:

try:
cursor = conn.cursor ()
cursor.execute ("SELECT * FROM dataset")
item = cursor.fetchone ()

Now, how do I step through the dataset one row at a time? My form has 'next'
and 'back' buttons, and I'd like them to step forward or back, fetching the
appropriate row in the table. I've tried setting cursor.rownumber by
incrementing it prior to the fetchone() w/o effect.

Thanks for any pointers.

Jeff Elkins
 
B

Benjamin Niemann

Jeff said:
I'm writing a small wxpython app to display and update a dataset. So far,
I get the first record for display:

try:
cursor = conn.cursor ()
cursor.execute ("SELECT * FROM dataset")
item = cursor.fetchone ()

Now, how do I step through the dataset one row at a time? My form has
'next' and 'back' buttons, and I'd like them to step forward or back,
fetching the appropriate row in the table. I've tried setting
cursor.rownumber by incrementing it prior to the fetchone() w/o effect.
You could either execute N-1 fetchone()s before you fetch the Nth dataset
(with N starting at 1)
or use the 'LIMIT' feature of MySQL:
cursor.execute ("SELECT * FROM dataset LIMIT %s,1", n)
where n is the index of the requested dataset (starting at 0)
 
W

wes weston

Jeff said:
I'm writing a small wxpython app to display and update a dataset. So far, I
get the first record for display:

try:
cursor = conn.cursor ()
cursor.execute ("SELECT * FROM dataset")
item = cursor.fetchone ()

Now, how do I step through the dataset one row at a time? My form has 'next'
and 'back' buttons, and I'd like them to step forward or back, fetching the
appropriate row in the table. I've tried setting cursor.rownumber by
incrementing it prior to the fetchone() w/o effect.

Thanks for any pointers.

Jeff Elkins
Jeff,
You just check for a fetchone return of None. "list" is a list
of tuples here.
...
cursor.execute( sql )
list = []
while 1:
row = cursor.fetchone()
if not row:
break
list.append(row)
...
wes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top