Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sqlite

size

blob

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 length(blob) won't work, because length() only works on texts and will stop counting after the first NULL. My empirical tests have shown this to be true.

I'm using SQLite 3.4.2


Updates:

So as of SQLite 3.7.6 it appears as though the length() function returns the correct value of blobs - I checked various change-logs of sqlite, but did not see in what version this was corrected.

From Sqlite 3.7.6:

 payload_id|length(payload)|length(hex(payload))/2 1807913|194|194 1807914|171|171 

The documentation was changed to reflect this.

 length(X)   The length(X) function returns the length of X in characters if X is             a string, or in bytes if X is a blob. If X is NULL then length(X) is             NULL. If X is numeric then length(X) returns the length of a string              representation of X. 
like image 814
Petriborg Avatar asked Oct 30 '08 17:10

Petriborg


People also ask

How do I know the size of my BLOB?

There is a simple MySQL String function, to find the size of BLOB data, OCTET_LENGTH. This function returns the length of BLOB in bytes. You can also use LENGTH function, because OCTET_LENGTH is synonym for LENGTH.

What is the size of BLOB in MySQL?

BLOB: Can handle up to 65,535 bytes of data. MEDIUMBLOB: The maximum length supported is 16,777,215 bytes. LONGBLOB: Stores up to 4,294,967,295 bytes of data.

How many characters can BLOB hold?

A BLOB (binary large object) is a varying-length binary string that can be up to 2,147,483,647 characters long.

What is BLOB in sqlite3?

BLOB stands for a binary large object that is a collection of binary data stored as a value in the database. By using the BLOB, you can store the documents, images, and other multimedia files in the database. We will create a new table named documents for the sake of demonstration.


2 Answers

haven't had this problem, but you could try length(hex(glob))/2

Update (Aug-2012): For SQLite 3.7.6 (released April 12, 2011) and later, length(blob_column) works as expected both both text and binary data.

like image 170
Javier Avatar answered Sep 18 '22 15:09

Javier


for me length(blob) works just fine, gives the same results like the other.

like image 20
Chris Avatar answered Sep 17 '22 15:09

Chris