How to search for a specifc BLOB in SQLite?

前端 未结 1 730
日久生厌
日久生厌 2021-01-14 11:39

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

CREATE TABLE pictures(id INTEGER PRIMARY KEY AUTOINCREMENT,          


        
相关标签:
1条回答
  • 2021-01-14 12:42

    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.

    0 讨论(0)
提交回复
热议问题