Skip to content Skip to sidebar Skip to footer

Python - Reading Blob Type From Sqlite3 Db

This is a follow on from: Python - Converting Hex to INT/CHAR I now have a working solution for converting the stored hex value of an IP from an sqlite3 db into a readable and usab

Solution 1:

After checking the link, it seems the most likely explanation is an empty row. On line 29 you set up a for loop to go over the results of a query. In python this means you have a list: [item,item2,item3] and each time you go through the loop, your row variable points to the next item.

Within the loop, you are checking the contents of the current item. Each of these items is a tuple which suppposedly has at least two entries. But if any of those items don't have an entry for row[0] or row[1] then you will get an index out of range exception.

You haven't posted enough code to make determining why this occurred feasible, but to get the code running I'd suggest this:

for row in cur.execute("select * from frames"):
    try:
        print row[0], str(row[1]).encode('hex') 
    except IndexError, e:
        print"IndexError: {0}".format(e)

That will continue across your entire query result, even if some of them are bad.

Edit: I just saw your update, and your problem is that c does not hold the contents of your query. c.execute('select ip from item where name = ' + "\'" + host + "\'") returns a list, which you ignore.

The original example works because you get the list in the for loop, and so it's an anonymous variable only within the loop context. To fix your code:

c = conn.cursor()
rows= c.execute('select ip from item where name = '+ "\'" + host + "\'")
forrowinrows:
        print row[0], str(row[1]).encode('hex')

Post a Comment for "Python - Reading Blob Type From Sqlite3 Db"