How to search for a specifc BLOB in SQLite?

白昼怎懂夜的黑 提交于 2019-12-01 05:57:35

问题


I write binary data from pictures into my SQLite database into a BLOB field called "icondata".

CREATE TABLE pictures(id INTEGER PRIMARY KEY AUTOINCREMENT, icondata BLOB)

The code snippet to write the binary data is:

SQLcommand.CommandText = "INSERT INTO pictures (id, icondata) VALUES (" & MyInteger & "," & "@img)"
SQLcommand.Prepare()
SQLcommand.Parameters.Add("@img", DbType.Binary, PicData.Length)
SQLcommand.Parameters("@img").Value = PicData

This works fine and I can find the BLOB values in the database using a tool like SQlite Spy.

But how can I search for a specific "icondata" BLOB?

If I try it with:

SQLcommand.CommandText = "SELECT id FROM pictures WHERE icondata=@img"

and with the same Parameters like above:

SQLcommand.Prepare()
SQLcommand.Parameters.Add("@img", DbType.Binary, PicData.Length)
SQLcommand.Parameters("@img").Value = PicData

Dim SQLreader As SQLite.SQLiteDataReader = SQLcommand.ExecuteReader()
While SQLreader.Read()
    ... Process found database entries....
End While

I don't get any result.

If I change the SELECT query to 'LIKE' instead of '=' I get all entries with BLOBs, not only the matching one.

How do I have to write the SELECT query to find the 1 exactly matching entry for a specific BLOB?


回答1:


You can search on BLOBs here's some examples :-

DROP TABLE IF EXISTS pictures;
CREATE TABLE IF NOT EXISTS pictures (id INTEGER PRIMARY KEY, icondata columntypedoesnotmatter);
INSERT INTO pictures (icondata) VALUES
    (x'fff1f2f3f4f5f6f7f8f9f0fff1f2f3f4f5f6f7f8f9f0'),
    (x'ffffffffffffffffffff'),
    (x'010203040506070809'),
    (x'010203040506070809010203040506070809')
    ;
SELECT id, hex(icondata) FROM pictures WHERE icondata = x'010203040506070809' OR icondata = x'FFFFFFFFFFFFFFFFFFFF';
SELECT id, hex(icondata) FROM pictures WHERE icondata LIKE '%'||x'F0'||'%';
SELECT id, hex(icondata) FROM pictures WHERE hex(icondata) LIKE '%F0%';

This results in :-

First query as expected finds the 2 rows (2nd and 3rd)

i.e. SELECT id, hex(icondata) FROM pictures WHERE icondata = x'010203040506070809' OR icondata = x'FFFFFFFFFFFFFFFFFFFF'; results in :-

The Second query does not work as expected and is an example of how not to use LIKE :- i.e. SELECT id, hex(icondata) FROM pictures WHERE icondata LIKE '%'||x'F0'||'%'; results in the unexpected two rows :-

  • I believe that this behaviour is due to only checking the higher/lower order bits but I can't find the relevant documentation.

The third query, however converts the stored blob into it's hexadecimal string representation using the hex function and compares that against the string representation of F0

i.e. SELECT id, hex(icondata) FROM pictures WHERE hex(icondata) LIKE '%F0%'; results in the expected single row :-

I've never used vb.net so I'm not sure how you'd code the above as passed parameters.



来源:https://stackoverflow.com/questions/54106519/how-to-search-for-a-specifc-blob-in-sqlite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!