Hi all,
I've been battling with this one for hours. I'm a relative Python newbie, but I do have some coding experience.
For clarity, I'll trim the connection stuff. I'm trying to do a lookup to get a number from one of my tables based on a given string (retrieved from a CSV file), something like:
This works, as long as there is a matching industry_group_name in the table. Where I am running into problems is when there isn't one -- according to the specs, sqlite3 returns None, which I understand is basically a Python Null type.Code:SELECT = 'select industry_group_num from tbl_INDUSTRY_GROUP where industry_group_name = ' + `IndGroupName` + ';'
Here's my supposed error catcher:
Code:result = cursor.execute(SELECT) if result.fetchone() is None: print 'Empty' else: var = result.fetchone()[0] print var
I have tried every variation I can think of for that if statement, but I always get this error from Python:
I figure that means that the None type is not a tuple and cannot be iterated through using [n] notation. But I can't figure out the solution!Code:TypeError: 'NoneType' object is unsubscriptable
Any and all help is appreciated. I hope that there's enough information; please let me know if there isn't.
Cheers!
togs
With a little help from another part of the Internet, a working solution. My original one wasn't that far off:
Code:SELECTstr = 'select industry_grp_num from INDUSTRY_GRP where industry_grp_name = ' + `IndGrpName` + ';' print SELECTstr cursor.execute(SELECTstr) result = cursor.fetchone() if result is None: print 'Empty' else: var = result[0] print var
Last edited by togs; 04-12-2009 at 09:01 AM. Reason: grammar
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks