Quoth Lynton Grice <[email protected]>, on 2011-04-03 12:37:06 +0200:
> There are NULL characters
You mean NUL characters.
> in the first couple header fields [...]
>
> I am no C expert but I have the following table defined:
>
> char *queueTable = "CREATE TABLE [test] ( "
> "[idx] INTEGER NOT NULL PRIMARY KEY
> AUTOINCREMENT, "
> "[raw_stream_in] BLOB, "
> "[num_bytes_in] INTEGER );";
I wouldn't recommend the use of [] for quoting identifiers (I'd prefer
double-quotes for more ANSI-esque SQL), but that looks like it should
work fine.
> sqlite3_bind_blob( stmt, idx, msg.raw_stream_in, msg.num_bytes_in,
> SQLITE_STATIC );
Be careful with SQLITE_STATIC. AIUI, the lifetime of the buffer must
then be a superset of the lifetime of the statement.
> For testing purposes I simply read the 15MB file from file into a char *.
>
> char *buffer = NULL;
> FILE *fp;
> if((fp = fopen("./in.txt", "r"))==NULL){
> exit(1);
> }
Not "rb" for binary mode?
> fseek(fp, 0, SEEK_END);
> long int fsize = ftell(fp);
> printf("File size: %i\n", fsize);
> rewind(fp);
> buffer = malloc(fsize * sizeof(char));
> fread(buffer, 1, fsize, fp);
> buffer[fsize] = '\0';
You're overwriting beyond the end of the array. You don't need an
extra NUL here.
> fclose(fp);
>
> I then point the MSG "raw_stream_in" to the buffer:
>
> msg_in.raw_stream_in = buffer;
And you set msg.num_bytes_in where?
> I then use the following statement as mentioned before to insert the
> stream into the BLOB field:
>
> sqlite3_bind_blob( stmt, idx, msg.raw_stream_in, msg.num_bytes_in,
> SQLITE_STATIC );
That looks fine by itself, subject to the caveats of SQLITE_STATIC
above and whether stmt and idx are valid.
> _So essentially all characters are copied until the first NULL
> char._
No. I strongly suspect that's a red herring.
In summary:
- Make sure msg.num_bytes_in is actually set to what you want.
- Make sure you're handling the lifetime of the buffer correctly;
for testing purposes I'd use SQLITE_TRANSIENT rather than
SQLITE_STATIC, since that evades that issue at the cost of a
memcpy.
- Don't write past the end of the array.
---> Drake Wilson
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users