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

Reply via email to