Hi there,

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

I have attached a file to show you that there are NULL characters in the first couple header fields and the rest is pure BINARY data. But just incase the image gets stipped off while posting 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

Reply via email to