Python - 从 SQLite3 DB 读取 BLOB 类型

发布于 2024-12-07 14:49:28 字数 1390 浏览 2 评论 0原文

这是以下内容的后续内容:Python - 将十六进制转换为 INT/CHAR

I现在有一个可行的解决方案,可以将存储的 IP 十六进制值从 sqlite3 数据库转换为可读且可用的格式。然而到目前为止,我一直通过直接从 sqlite3 数据库查看器复制和粘贴值来进行测试。

我一直在尝试使用脚本轮询数据库来查找该信息,但是我发现它显然将 blob 数据存储到人类无法立即读取的缓冲区中。

例如,IP 192.168.0.1 在 ip 字段下以 blob 类型存储为 X'C0A80001'

由于我将复制并粘贴的示例添加到我的代码中,所以我构建了代码以从十六进制中删除 X' 和 '然后能够将其转换。我无法从数据库中获取该值(X'C0A80001'<-我可以使用数据库管理器查看该值)。 搜索我发现 http: //eli.thegreenplace.net/2009/05/29/storing-blobs-in-a-sqlite-db-with-pythonpysqlite/ 其中显示了一个示例从 SQLite 数据库读取 Blob 或从 SQLite 数据库读取 Blob,但他们的方法不起作用。他们显示了错误;

打印 row[0], str(row[1]).encode('hex')
IndexError:元组索引超出范围

我对 Python 相当陌生,所以如果我错过了一些基本的东西,我深表歉意,但如果有人能帮助我理解这个问题,任何人提供的指针或代码示例将不胜感激。

编辑: 哦,没有粘贴上面示例中的代码;

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

这可能是我的理解,为什么我不能按照我想要

更新的方式读回它: 使用下面的答案我将代码修改为;

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

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")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够爱げ你 2024-12-14 14:49:28

检查链接后,最可能的解释似乎是空行。在第 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')

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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文