Python - Reading Blob Type from SQLite3 DB

This is the following: Python - Convert Hex to INT / CHAR

Now I have a working solution for converting the stored hexadecimal IP value from sqlite3 db to a readable and used format. However, so far I have tested by copying and pasting values ​​directly from sqlite3 db viewer.

I am trying to query db with a script to find this information, however I found that it automatically stores blob data in a buffer that is not instantly understandable to the user.

For example, IP 192.168.0.1 is stored as a blob type under the ip field as X'C0A80001 '

Since I was working with the copied and pasted example in my code, I created the code to remove X 'and' from the hex code, then to convert it. I cannot get this value from the database (X'C0A80001 '<- this is the value that I can view with the db manager). Search I found http://eli.thegreenplace.net/2009/05/29/storing-blobs-in-a-sqlite-db-with-pythonpysqlite/ , which showed an example of reading blobs in and out of sqlite db, but their methods did not work. They displayed an error;

print row[0], str(row[1]).encode('hex')
IndexError: tuple index out of range

I'm new to Python, so I apologize if I am missing something basic, but any pointers or code examples that should help someone understand me will be appreciated.

EDIT: Doh, did not paste the code from the above example;

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

, , , ,

UPDATE: , :

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

, . 29 for, . python , : [item,item2,item3] , , row .

. , , -, . - row[0] row[1], .

, , , , :

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)

, .

: , , c . c.execute('select ip from item where name = ' + "\'" + host + "\'") , .

, for, . :

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

Source: https://habr.com/ru/post/1535399/


All Articles