>>>>> Roger Binns writes:
>>>>> On 08/17/2011 09:25 PM, Ivan Shmakov wrote:

 >> Somehow, I've assumed that sqlite3_bind_blob () will bind a
 >> parameter to a blob.

 > It does.  There are no affinity rules that will cause otherwise.

 > There are some operations that cause blobs to be silently promoted to
 > strings.  IMHO these are egregious errors in SQLite since a bucket of
 > bytes cannot be turned into characters unless you know the encoding
 > which SQLite doesn't.  Here is an example:

 > sqlite> select X'616263' || 'd';
 > abcd
 > sqlite> select typeof(X'616263' || 'd');
 > text

        As a matter of personal preference, I'd be calling the code
        above “an error.”  It's not meaningful to my eye, and I'd likely
        avoid it in my code on the basis that even if it has some
        interpretation in the language, it'd be just too much a mental
        strain to remember such.

 > Consequently if you had a trigger pulling a stunt like this, your code
 > could try to insert a blob and silently (wrongly) end up with a string.
 > SQLite won't even complain if the blob isn't a valid text encoding
 > producing an invalid string.

        I don't have any triggers (at least, it wasn't my intent to add
        them.)  My code is roughly as shown below.

        And I don't seem to understand where's the problem.

        Also, I've built a simpler example following the same scheme
        (except the use of sqlite3_bind_parameter_index ()), and it
        doesn't seem to cast blobs to strings.

        Any suggestions?

        TIA.

   const char *const sql
     = ("INSERT"
        " INTO \"chunk\" (\"id\", \"length\", \"sha1\", \"sha256\")"
        " VALUES ($id, $length, $sha1, $sha256)");

   {
     ix_sha1
       = sqlite3_bind_parameter_index (st, "$sha1");
     assert (ix_sha1 > 0);
   }

   {
     int r
       = sqlite3_reset (st);
     assert (r == SQLITE_OK);
   }

   {
     int r
       = sqlite3_bind_blob (st, ix_sha1,
                            sha1, CHUNK_DB_SHA1_LEN,
                            SQLITE_TRANSIENT);
     assert (r == SQLITE_OK);
   }

   {
     int r
       = sqlite3_step (st);
     assert (r == SQLITE_DONE);
   }

[…]

-- 
FSF associate member #7257      Coming soon: Software Freedom Day
http://mail.sf-day.org/lists/listinfo/ planning-ru (ru), sfd-discuss (en)

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to