this is to all who have responded so far: thanks so much for your help so far: as a caveat, i am new to SQL so bear with me..
here is my code from the main.cpp where i build the short array: ostringstream insert; unsigned short *blobarray = new unsigned short[10]; //for simple purposes i will make this a small array. for(int i = 0; i < 10; i++) blobArray[i] = i * 3; insert << "insert into Images values(NULL, '" << Hello World << "')" long ID = db.performUpdate(insert2.str(), blobarray, 10); ...........outside of main......... long performUpdate(string update, const unsigned short * buffer, unsigned long size) { sqlite3_stmt *statement; open(filename); //this simply opens the db file, nothing big int status = sqlite3_prepare_v2(db, update.c_str(), -1, &statement, NULL); if((status != SQLITE_OK) || (statement == NULL)) cout << "error preparing" << endl; else cout << "preparing SQL statement successful" << endl; status = sqlite3_bind_blob(statement, 1, buffer, size * sizeof(unsigned short), SQLITE_STATIC); if(status != SQLITE_OK) cout << "status: " << status << " error binding blob. " << endl; else cout << "binding blob successful" << endl; while((status = sqlite3_step(statement)) == SQLITE_ROW); //check for status error like above status = sqlite3_finalize(statement); //check for status error like above // other non sqlite stuff...... } //end performUpdate do i need to create a column in table Images for the blob? or does this happen automatically? right now i have: create tables Images(ImageID integer primary key, messageID text not null); do i have to anything here for the blob? i might have to come back to understand more about the casting but right now i just wanted to show you my code. i hope it is readable and once again thanks for your help in advance! --- Fin Springs <[EMAIL PROTECTED]> wrote: > > On Feb 15, 2008, at 5:40 PM, C S usmsci-at-yahoo.com > |sqlite| wrote: > > > hi all i have a question regarding Blobs, that is > > storing images into the database. > > > > my image by default is an unsigned short array and > to > > bind blobs it wants a byte array. i am not sure i > am > > doing this right at all. is there a universal > cross > > platform way for this easy conversion? > > > > so far the code i have is: > > ......... > > char *byteArray = (char *) malloc((size * 2) * > > sizeof(char)); > > ......... > > > > for(unsigned int i = 0; i < index < size; i++) > > { > > byteArray[2 * index] = (char)buffer[index]; > > byteArray[(2 * index) + 1] = ( (char) > buffer[index] > >>> 1); > > > There is probably no reason to copy your data into a > separate char > array. Presuming you have something like: > > unsigned short myImage[]; > size_t size; // the number of elements in your > myImage array > > - you can just do: > > status = sqlite3_bind_blob(statement, 1, myImage, > size * > sizeof(unsigned short), SQLITE_STATIC or > SQLITE_TRANSIENT) > > Your choice for the last parameter will depend on > the life time of > 'myImage'; see > http://sqlite.org/c3ref/bind_blob.html > > > then i prepare the statement which is successful > then: > > > > status = sqlite3_bind_blob(statement, 1, > byteArray, > > size * 2, free); > > > > some questions i have: i get an error of '25' back > > from status and looking on the sqlite documention > it > > says the 2nd parameter to sql bind was out of > range. i > > have no idea how the 2nd parameter can be out of > > range. > You would need to share your statement definition. > Is there at least > one '?' token in it? > > > > > > my next question is once you have the blob in the > > database how in the world do you read it back out? > of > > course when i do read it back out i will need to > > convert it back to a short array to be able to use > it. > > > Use sqlite3_column_blob to get the pointer and > sqlite3_column_bytes to > know how much data there is (in bytes). You can just > cast the 'const > void *' pointer that you get back to a 'const > unsigned short *' and > divide the number of bytes there is by > sizeof(unsigned short) (ie 2) > to get the number of elements. > > _______________________________________________ > sqlite-users mailing list > sqlite-users@sqlite.org > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users > ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs _______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users