Python的 - 从sqlite3的DB读BLOB类型 [英] Python - Reading BLOB type from SQLite3 DB

查看:3145
本文介绍了Python的 - 从sqlite3的DB读BLOB类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个从遵循: Python的 - 十六进制转换为INT / CHAR

我现在有一种从sqlite3的分贝转换一个IP的存储十六进制值变为可读可用的格式一个可行的解决方案。但是到现在为止我已经被复制和直接从sqlite3的分贝观众粘贴值测试。

我一直在试图用一个脚本来查询数据库来查找信息,但是我发现它apparantly BLOB数据存储到一个缓冲区,不立即可读。

例如,192.168.0.1的IP是知识产权领域的X'C0A80001下存储为BLOB类型

由于我从复制并粘贴例如工作到我的code我已经建造code剥去X'和'从十六进制能够然后将其转换。我不能得到的是从数据库(X'C​​0A80001'< - 这个值,我可以用一个数据库管理器中查看),该值。
搜索我发现的
http://eli.thegreenplace.net/2009/05/29/storing-blobs-in-a-sqlite-db-with-pythonpysqlite/这表明阅读斑点,并从一个SQLite数据库的例子,但他们的方法没有奏效。它们显示的错误;


 打印行[0],STR(行[1])。EN code(十六进制)
IndexError:元组索引超出范围


我是相当新到Python所以道歉,如果我失去了一些东西基本的,但任何指针或code例子任何人有帮助我让我的头左右,这将是AP preciated。

编辑:
卫生署,没有粘贴在上面的例子中code;

  C = conn.cursor()
c.execute(从项目选择IP,其中name ='+\\'+主机+\\')
在C列:
        印刷行[0],STR(行[1])。EN code(十六进制)

这可能是我的理解这就是真正在这里下车,为什么我不能读回我想

更新:
使用下面的答案我修改了code到;

 行= c.execute('从项目选择IP,其中name ='+\\+主机+\\)
   在连续行:
       打印STR(行[0])。EN code(六角)


解决方案

检查链接后,它似乎是最可能的解释是一个空行。在第29行设置一个for循环,走过去一查询的结果。在蟒蛇,这意味着你有一个列表:
[项目,项目2,项目3] 并在每次经过循环的时候,你的变量指向下一个项目

在循环中,你正在检查当前项目的的内容的。这些物品是suppposedly具有至少两个条目的元组。但是,如果任何这些项目没有一个条目行[0] 行[1] 那么你将得到的指数超出范围的异常。

您还没有公布足够的code,使确定的为什么的出现这种情况是可行的,但要获得code运行我建议这样的:

 在cur.execute行(选择框*):
    尝试:
        印刷行[0],STR(行[1])。EN code(十六进制)
    除了IndexError,E:
        打印IndexError:{0}。格式(五)

这将继续在整个查询的结果,即使其中有些是不好的。

编辑:的我刚刚看到您的更新,以及您的问题是,C不抱你查询的内容。 c.execute('选择从项目知识产权,其中name ='+\\'+主机+\\')返回一个列表,你忽略了。

最初的例子作品,因为你得到的名单在for循环,所以它是一个匿名的变量的仅在循环背景的。要解决您的code:

  C = conn.cursor()
行= c.execute('从项目选择IP,其中name ='+\\'+主机+\\')
在连续行:
        打印行[0],STR(行[1])。EN code(十六进制)

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 usable format. However up until now I have been testing by copying and pasting the values directly from a sqlite3 db viewer.

I have been trying to poll the db with a script to lookup that information however I have discovered that it apparantly stores blob data into a buffer which isn't instantly human readable.

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

Since I worked from a copied and pasted example into my code I have constructed code to strip the X' and ' from the hex to be able to then convert it. What I cannot get is that value from the database (X'C0A80001' <- this value that I can view with a db manager). Searching I found http://eli.thegreenplace.net/2009/05/29/storing-blobs-in-a-sqlite-db-with-pythonpysqlite/ which showed an example of reading blobs to and from a sqlite db but their methods didn't work. They displayed the error;

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

I am fairly new to Python so apologies if I am missing something basic but any pointers or code examples anyone has to help me get my head around this would be appreciated.

EDIT: Doh, didn't 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')

It may be my understanding thats really off here as to why I can't read it back the way I want to

UPDATE: Using the below answer I modified my code to;

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

解决方案

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 + "\'")
for row in rows:
        print row[0], str(row[1]).encode('hex')

这篇关于Python的 - 从sqlite3的DB读BLOB类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆