C S wrote:
> hi all - i am trying once again to insert an image(an
> array of shorts) into a blob. 
> 
> i created a table that is described as such:
> 
> CREATE TABLE Images{
> imageID integer primary key,
> imageData text not null);
> 
> #1 - is text ok here for a blob? maybe not - this may
> be my entire problem. 
> 

I would recommend using a column type of BLOB for blob data just to 
avoid any confusion.

CREATE TABLE Images (
     imageID integer primary key,
     imageData blob not null);


> anyway instead of using data in an image i just made
> an array of unsigned shorts and filled it. a size of
> 10. 
> 
> so here is what i am doing in the code. imageArray
> holds the unsigned shorts(there are 10 of them)
> 
> char* blob = reinterpret_cast<char*>(imageArray);
> 

Generally you should use a void* instead of a char* for blobs.

void* blob = reinterpret_cast<void*>(imageArray);

This pointer isn't needed if your imageArray is a true array of unsigned 
shorts rather than a pointer to some dynamically allocated memory.

unsigned short imageArray[10] = {1,2,3,4,5,6,7,8,9,10};


> string myString = "insert into Images(ImageID,
> imageData) values(?, 'blob')";

Note, this 'blob' is a string literal and has no relation at all to the 
blob variable you defined above. You need to use a ? for the second 
parameter as well so that you can bind a value to it later.

string myString = "insert into Images(ImageID, imageData) values(?, ?)";

> 
> //then i want to try to prepare the statement:
> 
> int status = sqlite3_prepare_v2(db, myString.c_str(),
> -1, &statement, NULL);
> if( (status != SQLITE_OK) || (statement == NULL))
>   cout << "Error preparing SQL Statement" << endl;
> 

There is no need to check statement here. SQLite will return an error 
code other than SQLITE_OK if it fails.

> 
> //now i would like to bind the blob:
> 
> status = sqlite3_bind_blob(statement, 1, imageArray,
> 10 * sizeof(unsigned short), SQLITE_TRANSIENT);
> 

The blob will be the second parameter to the statement. This parameter 
can be static as long as the image data will be stable until the 
statement is executed by sqlite3_step().

If you want to use the blob pointer above you should do this:

status = sqlite3_bind_blob(statement, 2, blob,
     10 * sizeof(unsigned short), SQLITE_STATIC);

If imageArray is a true array as shown above you can instead do this:

status = sqlite3_bind_blob(statement, 2, imageArray,
     sizeof(imageArray), SQLITE_STATIC);

You have not bound a value to the first parameter, the imageId column, 
so it will have a null value when the statement executes. This is OK 
since the column is declared as "integer primary key" and SQLite will 
assign a unique ID value.

> 
> //execute statement for each row??
> while( (status = sqlite3_step(statement)) ==
> SQLITE_ROW);
> 

There is no need for a while loop here. An insert statement can only 
step once, and sqlite3_step() will return SQLITE_DONE or some other 
error code.

> //free the prepared statement
> status = sqlite3_finalize(statement);
> if(status != SQLITE_OK)
>   cout << "Error deleting prepared SQL statement" <<
> endl;
> =====================================================
> 
> i actually get the last status check output, saying
> that there was an error deleting the statement. when i
> looked that up it says there was a problem with the
> prepare statement being successful or nothing happens
> at all when clearly i did and clearly the status was
> ok too since i didnt get an error message there. 
> 

I'm not sure why the finalize would fail, unless perhaps your prepare 
also failed, and you didn't have a valid statement pointer to pass to 
sqlite3_finalize().

> can anyone help me out as to what might be going on
> here? thanks so much in advance 
> 

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

Reply via email to