Python: - 如何在 SQLite3 DB 中搜索 Blob 类型?
继自;
Python - 从 SQLite3 DB 读取 BLOB 类型
我有一个脚本现在可以查询具有给定 id 的 sqlite3 数据库。从这个 id 中可以找到该设备的 IP。这一切都是在表 1 中完成的。然后,它使用找到的 IP 并转到表 2。
然后,它会尝试在 table2 中搜索具有与先前发现的 IP 匹配的字段 IP 的行。
感谢前面问题的帮助,我已经成功地将 IP 存储在 sqlite3 数据库中的原始 blob 类型转换为可读格式(转换为十六进制,然后转换为实际 IP)。现在我发现我实际上无法执行 sql where 命令来搜索它。 也许我天真地尝试从第一次搜索中搜索原始返回值,但这会返回一个缓冲区,
(<read-write buffer ptr 0x7f251c86d778, size 4 at 0x7f251c86d738>,)
这显然是我不能用来搜索 blob 的精确副本的东西,因为它包含其他信息。我想到的唯一解决方案是遍历数据库并将每个 IP 从 blob 转换为十六进制,并与我之前存储的变量进行比较,其中它已从上一个表转换为十六进制。
这是我执行此类搜索的唯一方法吗?今天我在这方面得到了很多帮助,所以我很高兴得到正确方向的指导
代码摘录: 这将返回第一个表中的 IP
rows = c.execute('select ip from table1 where name= ' + "\"" + host + "\"")
for row in rows:
print str(row[0]).encode("hex")
Following on from;
Python - Reading BLOB type from SQLite3 DB
Python - Converting Hex to INT/CHAR
I have a script that now queries a sqlite3 database with a given id. From this id it finds the IP of that device. This is all done within table1. It then takes this found IP and goes to table2.
Within table2 it then tries to do a search for rows which have the field IP that matches the previously discovered IP.
Thanks to help in the previous questions I have managed to convert the original blob type that the IP is stored in the sqlite3 database into a readable format (into hex and then into an actual IP). Now I have found that I can't actually do an sql where command to then search for this.
Perhaps naively I tried to do a search for the original return value from my first search however this returns a buffer
(<read-write buffer ptr 0x7f251c86d778, size 4 at 0x7f251c86d738>,)
This is something I can't obviously use to search for an exact copy of the blob as it contains other information. The only solution I have thought of is to traverse the db and convert each IP from a blob into hex and do a comparison against my previously stored variable where it has been converted into hex from the previous table.
Is this the only way I can perform a search like this? I have enjoyed a lot of help on this today so I am happy for pointers in the right direction
Code Excerpt:
This returns the IP from the first table
rows = c.execute('select ip from table1 where name= ' + "\"" + host + "\"")
for row in rows:
print str(row[0]).encode("hex")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论