[newbie] Right way to access item in array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gilles Ganault

    #1

    [newbie] Right way to access item in array?

    Hello

    I'd like to know what the right way is to access an item in a row as
    returned by a database:

    =====
    import apsw

    connection=apsw .Connection("te st.sqlite")
    cursor=connecti on.cursor()

    rows=cursor.exe cute("SELECT isbn,price FROM books WHERE price IS
    NULL")
    for row in rows:

    #Is this right?
    for isbn in row:

    if isbn:
    print "Found price = " + price

    connection.clos e(True)
    =====

    Thank you.
  • Diez B. Roggisch

    #2
    Re: [newbie] Right way to access item in array?

    Gilles Ganault wrote:
    Hello
    >
    I'd like to know what the right way is to access an item in a row as
    returned by a database:
    >
    =====
    import apsw
    >
    connection=apsw .Connection("te st.sqlite")
    cursor=connecti on.cursor()
    >
    rows=cursor.exe cute("SELECT isbn,price FROM books WHERE price IS
    NULL")
    for row in rows:
    >
    #Is this right?
    for isbn in row:
    No. This will iterate though all columns, not just the isbn.

    You can use tuple-unpacking in such cases:

    for row in rows:
    isbn, price = row
    ...


    Diez

    Comment

    • =?ISO-8859-1?Q?Gerhard_H=E4ring?=

      #3
      Re: [newbie] Right way to access item in array?

      Diez B. Roggisch wrote:
      Gilles Ganault wrote:
      >
      >Hello
      >>
      >I'd like to know what the right way is to access an item in a row as
      >returned by a database:
      >>
      >=====
      >import apsw
      >>
      >connection=aps w.Connection("t est.sqlite")
      >cursor=connect ion.cursor()
      >>
      >rows=cursor.ex ecute("SELECT isbn,price FROM books WHERE price IS
      >NULL")
      >for row in rows:
      >>
      >#Is this right?
      >for isbn in row:
      >
      No. This will iterate though all columns, not just the isbn.
      >
      You can use tuple-unpacking in such cases:
      >
      for row in rows:
      isbn, price = row
      ...
      You can do it even in one step with APSW (and pysqlite, and others):

      for isbn, price in cur.execute("se lect isbn, price ..."):
      ....

      -- Gerhard

      Comment

      • Steve Holden

        #4
        Re: [newbie] Right way to access item in array?

        Gilles Ganault wrote:
        Hello
        >
        I'd like to know what the right way is to access an item in a row as
        returned by a database:
        >
        =====
        import apsw
        >
        connection=apsw .Connection("te st.sqlite")
        cursor=connecti on.cursor()
        >
        rows=cursor.exe cute("SELECT isbn,price FROM books WHERE price IS
        NULL")
        If you are dealing with a DB API-compliant module then the return value
        from the cursor's execute method is undefined, and you need to call one
        of the "fetch" methods to extract the retrieved data.

        So you would want something like

        cursor.execute( "SELECT isbn,price FROM books WHERE price IS NULL")
        rows = cursor.fetchall ()
        for isbn, price in rows:
        print isbn, ":", price

        Once you get that working you can do your own computations with isbn and
        price. Note, however, that the specific query you use guarantees that
        the value of "proce" will be None, since you only retrieve the rows
        where price is NULL!
        for row in rows:
        >
        #Is this right?
        for isbn in row:
        >
        if isbn:
        print "Found price = " + price
        >
        connection.clos e(True)
        >
        regards
        Steve
        --
        Steve Holden +1 571 484 6266 +1 800 494 3119
        Holden Web LLC http://www.holdenweb.com/

        Comment

        • Gilles Ganault

          #5
          Re: [newbie] Right way to access item in array?

          On Tue, 28 Oct 2008 12:12:26 +0100, Gerhard Häring <gh@ghaering.de >
          wrote:
          >You can do it even in one step with APSW (and pysqlite, and others):
          >
          >for isbn, price in cur.execute("se lect isbn, price ..."):
          Thanks much guys. For those interested, here's some working code:

          ======
          import apsw

          connection=apsw .Connection("my books.sqlite")
          cursor=connecti on.cursor()

          for id, isbn in list(cursor.exe cute("SELECT id,isbn FROM books WHERE
          price IS NULL")):

          print isbn

          connection.clos e(True)
          ======

          HTH,

          Comment

          • Gilles Ganault

            #6
            Re: [newbie] Right way to access item in array?

            On Tue, 28 Oct 2008 09:56:11 -0400, Steve Holden <steve@holdenwe b.com>
            wrote:
            >If you are dealing with a DB API-compliant module then the return value
            >from the cursor's execute method is undefined, and you need to call one
            >of the "fetch" methods to extract the retrieved data.
            Thanks for pointing it out. I read that calling list() on the dataset
            is the way to go with this module:

            for id, isbn in list(cursor.exe cute("SELECT id,isbn FROM books WHERE
            price IS NULL")):

            Comment

            Working...