Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

SQLite/P2.6: Identifiying when execute() returns None

sqlite

  • Please log in to reply
1 reply to this topic

#1 togs

togs

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 12 April 2009 - 06:21 AM

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:

SELECT = 'select industry_group_num from tbl_INDUSTRY_GROUP where industry_group_name = ' + `IndGroupName` + ';'

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.

Here's my supposed error catcher:

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:

TypeError: 'NoneType' object is unsubscriptable

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!

Any and all help is appreciated. I hope that there's enough information; please let me know if there isn't.

Cheers!
togs
  • 0

#2 togs

togs

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 12 April 2009 - 08:01 AM

With a little help from another part of the Internet, a working solution. My original one wasn't that far off:

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

Edited by togs, 12 April 2009 - 08:01 AM.
grammar

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download