Walking through a mysql db

Discussion in 'Python' started by Jeff Elkins, Jun 4, 2005.

  1. Jeff Elkins

    Jeff Elkins Guest

    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 Elkins, Jun 4, 2005
    #1
    1. Advertising

  2. Jeff Elkins wrote:

    > 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)

    --
    Benjamin Niemann
    Email: pink at odahoda dot de
    WWW: http://www.odahoda.de/
    Benjamin Niemann, Jun 4, 2005
    #2
    1. Advertising

  3. Jeff Elkins

    wes weston Guest

    Jeff Elkins wrote:
    > 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
    wes weston, Jun 4, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Elkins

    Re: Walking through a mysql db

    Jeff Elkins, Jun 4, 2005, in forum: Python
    Replies:
    2
    Views:
    431
    Dennis Lee Bieber
    Jun 4, 2005
  2. Thierry Lam

    Walking through directories and files

    Thierry Lam, Sep 16, 2005, in forum: Python
    Replies:
    1
    Views:
    279
    Fredrik Lundh
    Sep 16, 2005
  3. gaga
    Replies:
    19
    Views:
    532
  4. Xeno Campanoli
    Replies:
    3
    Views:
    98
    Xeno Campanoli
    Jun 15, 2007
  5. Replies:
    7
    Views:
    99
Loading...

Share This Page