wxSqlite3 + wxRichTextCtrl content save Topic is solved

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
Allonii
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 24, 2009 1:28 pm

wxSqlite3 + wxRichTextCtrl content save

Post by Allonii »

Hello!

I have a problem when it comes to saving the content of a wxRichTextCtrl to a wxsqlite3 database.

The problem is that images never work, text works sometimes, sometimes my program just crashes. I constantly get the error "XML parsing error:'not well-formed (invalid token)'"
In other words its very buggy and I don't understand why.

(the problem is related to wxsqlite3 because when I just save to a stream and then put it back everything works. So there is something wrong with the db code?)

Any help is appreciated.

Code: Select all

//I have already added the wxRichTextXMLHandler

//Getting the content of the wxRichTextCtrl
wxMemoryOutputStream stream;
richTxtCtrl->GetBuffer().SaveFile(stream,wxRICHTEXT_TYPE_XML);
unsigned char* data = new unsigned char[stream.GetSize() + 1];
stream.CopyTo(data, stream.GetSize());
save(_T("test1"),data,stream.GetSize())

Code: Select all

//Database save
void save(wxString& name, unsigned char* data, unsigned int dataLen){
    wxSQLite3Database* db = new wxSQLite3Database();
    db->Open(_T("database.db"));
    //NULL is the id.
    wxSQLite3Statement stmt = db->PrepareStatement("INSERT INTO table1 VALUES (NULL, ?, ?);");
    stmt.Bind(1, name);
    stmt.Bind(2,data,dataLen);
    stmt.ExecuteUpdate();
    stmt.ClearBindings();
    stmt.Reset();
    db->Close();
    delete db;
}

Code: Select all

//Database load
//RichContent is a struct with data and dataLen as members.
RichContent load(wxString& name){
    wxSQLite3Database* db = new wxSQLite3Database();
    db->Open(_T("database.db"));
    //I only have one entry in the db. 
    wxString querry = wxString::Format(_T("SELECT content FROM table1 WHERE name = '%s';"),name);
    wxSQLite3ResultSet rSet = db.ExecuteQuery(querry);
    int dataLen;
    const unsigned char* data = rSet.GetBlob(1,dataLen);
    RichContent rCont = {data, dataLen};
    rSet.Finalize();
    db->Close();
    delete db;
    return rCont;
}

Code: Select all

//Reading the wxRichTextCtrl content back
RichContent rCont = load(_T("test1"));
wxMemoryInputStream stream(rCont.data, rCont.dataLen);
wxRichTextBuffer& buffer = richTxtCtrl->GetBuffer();
buffer.LoadFile(stream,wxRICHTEXT_TYPE_XML);
richTxtCtrl->Refresh();
Thanks

Allonii
Allonii
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 24, 2009 1:28 pm

Post by Allonii »

Another topic asked and answered by me. Maybe I should post my topics in some tutorial because I ask and ending up answering them myself. :D

Anyway the solution is just to replace the MemoryXXXStream by wxStringXXXStream. Very Simple, this is because the xml break sometimes when transferring it to raw data.
Thanks

Allonii
Post Reply