Inserting binary data into an sqlite database using C/C++ interface:
// Previously: "create table tt(i integer primary key, b blob)"
sqlite3 *sqliteDB;
if(sqlite3_open("example.db", &sqliteDB) == 0)
{
// "insert" statement
const char *sqlText = "insert into tt (i,b) values (?,?)";
sqlite3_stmt *preparedStatement;
sqlite3_prepare_v2(sqliteDB, sqlText, (int)strlen(sqlText), &preparedStatement, 0);
// add the number
sqlite3_bind_int(preparedStatement, 1, 1);
// add the data
char myBlobData[] = "He\0llo world"; // sizeof(myBlobData) is 13 bytes
sqlite3_bind_blob(preparedStatement, 2, myBlobData, sizeof(myBlobData), 0); // returns SQLITE_OK
// store in database
sqlite3_step(preparedStatement); // returns SQLITE_DONE
sqlite3_close(sqliteDB);
}
This stores a 2-byte blob containing "He", instead of the 13-byte blob that it was asked to store. Why?
It seems to be ending at the first 0 byte, but blobs aren't strings (hence the need for a size parameter to sqlite3_bind_blob) so why would it treat arbitrary binary data as a c-style string?