Closed Thread
Results 1 to 2 of 2

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

  1. #1
    togs is offline Newbie
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

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

    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:

    Code:
    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:

    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:

    Code:
    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    togs is offline Newbie
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

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

    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

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help with SQLite Database Browser?
    By dizzynumbers in forum General Programming
    Replies: 4
    Last Post: 04-10-2011, 01:23 PM
  2. Javascript SQLite HTML5
    By markeh in forum JavaScript and CSS
    Replies: 1
    Last Post: 03-04-2011, 03:27 AM
  3. Releasing project with SQLite
    By nick3 in forum C# Programming
    Replies: 3
    Last Post: 02-16-2010, 07:04 AM
  4. Linux SQLite client?
    By Hektor in forum Linux Applications
    Replies: 12
    Last Post: 12-03-2008, 04:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts