How do I find the length (size) of a binary blob in sqlite

后端 未结 5 1695
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 09:00

I have an sqlite table that contains a BLOB file, but need to do a size/length check on the blob, how do I do that?

According to some documentation I did find, using

5条回答
  •  离开以前
    2020-12-16 09:34

    As an additional answer, a common problem is that sqlite effectively ignores the column type of a table, so if you store a string in a blob column, it becomes a string column for that row. As length works different on strings, it will then only return the number of characters before the final 0 octet. It's easy to store strings in blob columns because you normally have to cast explicitly to insert a blob:

    insert into table values ('xxxx'); // string insert
    insert into table values(cast('xxxx' as blob)); // blob insert
    

    to get the correct length for values stored as string, you can cast the length argument to blob:

    select length(string-value-from-blob-column); // treast blob column as string
    select length(cast(blob-column as blob)); // correctly returns blob length
    

    The reason why length(hex(blob-column))/2 works is that hex doesn't stop at internal 0 octets, and the generated hex string doesn't contain 0 octets anymore, so length returns the correct (full) length.

提交回复
热议问题