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

In this forum you can discuss database related issues which can be wxWidgets related, but also generic in nature.
dkaip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Wed Jan 20, 2010 1:15 pm

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

Postby dkaip » Sat Oct 21, 2017 9:56 am

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;

User avatar
doublemax
Moderator
Moderator
Posts: 11527
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

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

Postby doublemax » Sat Oct 21, 2017 11:07 am

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();
Use the source, Luke!

dkaip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Wed Jan 20, 2010 1:15 pm

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

Postby dkaip » Sat Oct 21, 2017 11:48 am

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());

dkaip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Wed Jan 20, 2010 1:15 pm

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

Postby dkaip » Wed Oct 25, 2017 5:48 am

I quit it. Instead of learning, I found another more correct method, saving the picture.
Thanks for help.
Jim

User avatar
marcelinux
Knows some wx things
Knows some wx things
Posts: 33
Joined: Thu Nov 07, 2013 9:59 pm
Location: Madrid, Spain

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

Postby marcelinux » Wed Oct 25, 2017 8:03 am

dkaip wrote:I found another more correct method, saving the picture.

What, please? [-o<
I just need learn a little bit more. Thank you for your help.


Return to “Database Related”

Who is online

Users browsing this forum: No registered users and 1 guest