From: Chris Hare on 5 Aug 2010 10:26 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]
From: Benjamin Kaplan on 5 Aug 2010 10:54 On Thu, Aug 5, 2010 at 7:26 AM, Chris Hare <chare(a)labr.net> wrote: > > 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] > -- netNumber, netType, netConditions, netStartLocal, NCS, NCS1, RADAR, NetFreq, Repeater, Notes = list by the way, don't call the list "list". It will hide the built-in with the same name. > http://mail.python.org/mailman/listinfo/python-list >
From: Tim Chase on 5 Aug 2010 10:59 On 08/05/10 09:26, Chris Hare wrote: > 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? > > cursor.execute('select * from net where NetNumber> 0') > list = cursor.fetchone() First off, I'd not mask the built-in "list", but you can use row = cursor.fetchone() if row: (netNumber, netType, netCond, netStLo, NCS, NCS1) = row else: print "No results" (just add in the right number of variables to which you want to assign to) -tkc
|
Pages: 1 Prev: abstract metaclass Next: easy question on parsing python: "is not None" |