Page 1 of 1

How i can save the char data directly to blob db?

Posted: Sat Oct 21, 2017 9:56 am
by dkaip
I need to save an wxBitmap to SQLite to a blob type without save to disk or on some media.
i have mybitmap as wxBitmap;
First must convert to wxImage and get data.
Ok until now i hope, but how i can save the char data directly to blob db?
In sql must have
update mytable SET blob_column='??????' WHERE id='1'
or something like that.
How must construct query?

Code: Select all

wxImage img=mybitmap.ConvertToImage();
unsigned char* data=img.GetData();
int datalength=img.GetWidth()*img.GetHeight()*3;
char *myBuffer = (char *)data;

Re: How i can save the char data directly to blob db?

Posted: Sat Oct 21, 2017 11:07 am
by doublemax

Code: Select all

wxImage img=mybitmap.ConvertToImage();
unsigned char* data=img.GetData();
int datalength=img.GetWidth()*img.GetHeight()*3;
This is not a good solution. You lose the alpha channel if there is one. And more importantly, you have no information about width and height in the data buffer. It's better to save the image into a memory buffer and then save the content of that buffer.

Some code stripped together blindly. Not sure if it compiles, but you should get the idea:

Code: Select all

#include <wx/mstream.h> // for wxMemoryOutputStream

wxImage img(...);

wxMemoryOutputStream mo;
img.SaveFile( mo, wxBITMAP_TYPE_PNG );

wxSQLite3Statement stmt = db.PrepareStatement("UPDATE mytable SET blob_column='?' WHERE id='1'");
stmt.Bind(0, (unsigned char *)mo.GetOutputStreamBuffer()->GetBufferStart(), mo.GetOutputStreamBuffer()->GetBufferSize());
stmt.ExecuteUpdate();

Re: How i can save the char data directly to blob db?

Posted: Sat Oct 21, 2017 11:48 am
by dkaip
Thank you doublemax. You have right. Seam that is the only good way. I can not use wxSQLite3, but SQLite, so i must change the construct of querty.
Thanks, because i had big problem. If i made the code, i will upload.
Some other code from net..

Code: Select all

    wxSQLite3Blob picblob = db.GetBlob(SpinCtrl1->GetValue(),"Picture","Customers");
    wxMemoryBuffer picload;
    int blobsize = picblob.GetSize();
    picblob.Read(picload, blobsize, 0);

    void* dat = malloc(picload.GetDataLen());
    memcpy(dat, picload.GetData(), blobsize);

    picture.SetData((unsigned char*) dat, picload.GetDataLen());

Re: How i can save the char data directly to blob db?

Posted: Wed Oct 25, 2017 5:48 am
by dkaip
I quit it. Instead of learning, I found another more correct method, saving the picture.
Thanks for help.
Jim

Re: How i can save the char data directly to blob db?

Posted: Wed Oct 25, 2017 8:03 am
by marcelinux
dkaip wrote:I found another more correct method, saving the picture.
What, please? [-o<