assigning variables from list data

Discussion in 'Python' started by Chris Hare, Aug 5, 2010.

  1. Chris Hare

    Chris Hare Guest

    I have a database query result (see code below). In PHP, I would have said

    list(var1,var2,var) = $result

    and each element in the list would be assigned to each of the named variables. I have my data coming out of the database, and I can see it is a list. so my question is, instead of having to do the variable assignment as I have it here, is there a way more like PHP or am I stuck with it?

    import sqlite3 as sqlite

    try:
    print "connecting to disk db ..."
    conn = sqlite.connect("netcomm.db")
    except:
    print "oops"

    print "retrieving data"
    cursor = conn.cursor()
    cursor.execute('select * from net where NetNumber > 0')
    list = cursor.fetchone()
    print list
    print len(list)
    for item in list:
    print item
    netNumber = list[0]
    netType = list[1]
    netConditions = list[2]
    netStartLocal = list[3]
    NCS = list[4]
    NCS1 = list[5]
    RADAR = list[6]
    NetFreq = list[7]
    Repeater = list[8]
    Notes = list[9]
     
    Chris Hare, Aug 5, 2010
    #1
    1. Advertising

  2. Chris Hare a écrit :
    > I have a database query result (see code below). In PHP, I would have said
    >
    > list(var1,var2,var) = $result


    Other already answered on the Python equivalent. But there's an IMHO
    better way, which is to use (if the DB-API connector provides it) a
    DictCursor, that yields dicts instead of tuples.

    >
    > import sqlite3 as sqlite
    >
    > try:
    > print "connecting to disk db ..."
    > conn = sqlite.connect("netcomm.db")
    > except:
    > print "oops"



    OT, but do yourself a favor and remove this try/except. Sorry to have to
    say but it's about the most stupidiest error handling scheme you could
    think of. Seriously.

    Ask yourself : what will happens if sqlite.connect raises AND you don't
    catch the exception ? Yes, the script will stop right here - which is
    what you want anyway - AND you will have an error message giving you
    informations about what went wrong, and a full traceback. IOW, you'll
    have as much helpful debugging infos as possible at this point.

    Now what will happen with your current code ? Instead of an informative
    error message, you have a "oops". Well, really helps. Then your script
    will go on, print "retrieving data", and you can bet your ass it'll
    crash on the next line - "cursor = conn.cursor()", that is - with a very
    less informative error message, and you'll wonder why.

    Also, be warned that SysExit and KeyboardInterrupt are also exceptions.
    The one you may NOT want to catch and dismiss.

    To make a long story short:

    1/ only catch exceptions you can *and* want to handle
    2/ never assume anything about what caused the exception
    3/ not exception handling is better than a wrong exception handling.

    HTH
     
    Bruno Desthuilliers, Aug 6, 2010
    #2
    1. Advertising

  3. Chris Hare

    Aahz Guest

    In article <>,
    Chris Hare <> wrote:
    >
    >cursor.execute('select * from net where NetNumber > 0')


    Unless your table is guaranteed to never change layout, I suggest that
    instead listing fields is a Good Idea:

    cursor.execute('select netNumber, netType, .... from net where NetNumber > 0')

    Then the tuple unpack suggested by other posters will never fail.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "...if I were on life-support, I'd rather have it run by a Gameboy than a
    Windows box." --Cliff Wells
     
    Aahz, Aug 18, 2010
    #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. mhnazly
    Replies:
    1
    Views:
    691
    Mike Moore [MSFT]
    Oct 28, 2003
  2. Michael
    Replies:
    1
    Views:
    840
    =?Utf-8?B?RFdT?=
    Jan 30, 2006
  3. fAbs
    Replies:
    17
    Views:
    573
  4. Wells
    Replies:
    1
    Views:
    321
    Chris Rebert
    May 14, 2009
  5. weston
    Replies:
    1
    Views:
    281
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page