You apparently don't understand "strings" in C.  Or are you actually reading in 
binary data?

#1 Since you said "image" I assume you're reading binaary. So get rid of 
buffer[fsize]=0.  You don't null terminate binary data and that statement is 
1-beyond the end of the array (which is from 0 to fsize-1).

#2 You then say you "point the MSG".  I suspect you're doing that wrong with 
strcpy() or such.  So if you would care to show ALL your code (it's just a few 
more lines) we can probably help.

#3 Also, what happens to "buffer"?? Do you free it at some point after the 
bind?  If so, that 



Michael D. Black
Senior Scientist
NG Information Systems
Advanced Analytics Directorate



________________________________________
From: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on 
behalf of Lynton Grice [lynton.gr...@logosworld.com]
Sent: Sunday, April 03, 2011 5:37 AM
To: sqlite-users@sqlite.org
Subject: EXT :[sqlite] sqlite3_bind_blob CHOPS off at first NULL char

  Hi there,

I have a 15MB file I need to read and store in an SQLite database.

  There are NULL characters in the first couple header fields and the
rest is pure BINARY data. You can imagine the file looking like:

ppphNULNUL3STR.....and then all the BINARY data......

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 );";

And then later in the code I use the "sqlite3_bind_blob" to send the
stream to SQLIte

sqlite3_bind_blob( stmt, idx, msg.raw_stream_in, msg.num_bytes_in,
SQLITE_STATIC );

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);
   }

   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';
   fclose(fp);

I then point the MSG "raw_stream_in" to the buffer:

msg_in.raw_stream_in = buffer;

NOTE: Msg is a "message" as defined below:

typedef struct messageStruct {
char *raw_stream_in;
   int num_bytes_in;
}message;

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 );

However, _only the first 4 bytes are copied_ (the "ppph" in the attached
image). _So essentially all characters are copied until the first NULL
char._

How can I store EVERYTHING, including NULLs? Must I use a BYTE array or
something? Does anyone have any sample code?

I would be hugely appreciative for any help in this regard, thanks

Lynton

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

Reply via email to