How to search for a specifc BLOB in SQLite?

谁说我不能喝 提交于 2019-12-01 09:10:26

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.

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