in reply to Re: How to store NUL characters into SQLite BLOB ?
in thread How to store NUL characters into SQLite BLOB ?

I've tracked down the problem a bit deeper, and determined that it indeed is a problem with DBD::SQLite. Contrary to the other comments on this page, it is not a problem with retrieval but with storage. Blobs set by outside means are retrieved correctly. Currently, the full NUL containing BLOB is being stored, but since it be being bound with 'sqlite3_bind_text()' the SQLite internal type is being set to text, and then the subsequent retrieve fails.

The solution is use sqlite3_bind_blob() to bind the variable. There is is currently code in dbdimp.c that appears to do this, but it never is called. I'm not sure if this is due to a bug or something not implemented at a higher level. I've come up with a a workaround patch, but I don't understand the internals well enough to know whether it is a good idea.

--- dbdimp.c.orig       2005-11-10 19:27:55.000000000 -0700
+++ dbdimp.c    2005-11-10 19:27:32.000000000 -0700
@@ -356,11 +356,17 @@
             STRLEN len;
             char * data = SvPV(value, len);
             retval = sqlite3_bind_blob(imp_sth->stmt, i+1, data, len, SQLITE_TRANSIENT);
-        }
+        } 
         else {
             STRLEN len;
             char * data = SvPV(value, len);
-            retval = sqlite3_bind_text(imp_sth->stmt, i+1, data, len, SQLITE_TRANSIENT);
+           if (memchr(data, 0, len)) {
+               sqlite_trace(4, "binding NUL containing data as a blob");
+               retval = sqlite3_bind_blob(imp_sth->stmt, i+1, data, len, SQLITE_TRANSIENT);
+           }
+           else {
+               retval = sqlite3_bind_text(imp_sth->stmt, i+1, data, len, SQLITE_TRANSIENT);
+           }
         }
  • Comment on Re^2: How to store NUL characters into SQLite BLOB ?