I've been stuck for a while on a problem and can't find a good solution.
I wrote a small class to use the c mysql lib on my project.
I use this class from an other class, the compilation goes well, but when i call it the software crash.
I believe that i'm doing something wrong but I can't find what. Maybe some advanced ppl will know.
Sql class:
class MySQL
{
public:
MySQL();
~MySQL();
bool Query(wxString Qu, MYSQL_RES * result);
unsigned int Insert(wxString Table);
bool Update(wxString Table, unsigned int Id, wxString Data);
bool Delete(wxString Table, unsigned int Id);
protected:
private:
void Close(void);
bool Open(void);
MYSQL mysql;
};
Sql function:
bool MySQL::Query(wxString Qu, MYSQL_RES * result)
{
//Qu.mb_str()
if (this->Open()) {
if (!mysql_query(&mysql, "jkjl")) { //<--- I've replaced the Qu var to be sure it wasn't the reason.
if ((result = mysql_store_result(&mysql))) {
this->Close();
}
else {
//throw wxString::Format(wxT("Error: %i, %s"), mysql_errno(&mysql), mysql_error(&mysql));
this->Close();
}
}
else {
//throw wxString::Format(wxT("Error: %i, %s"), mysql_errno(&mysql), mysql_error(&mysql));
this->Close();
}
}
return true;
}
My other class to access to:
bool cldataproject::Load(std::list<cldataproject*> list_dataproject)
{
MySQL sql;
MYSQL_RES *result = NULL;
MYSQL_ROW row = NULL;
unsigned int pos;
cldataproject * list_project;
wxString req;
req = wxT("SELECT idproject, idclient, name, comment, created_by, created_at, modified_by, modified_at FROM project WHERE isdel=0 ORDER BY name ASC;");
if ((sql.Query(req, result))) // <--- Crash
{
......
Open function:
bool MySQL::Open(void)
{
mysql_init(&mysql);
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"option");
if(mysql_real_connect(&mysql,MYSQL_HOST,MYSQL_LOGIN,MYSQL_PASS,MYSQL_BASE,0,NULL,0))
{
return true;
}
else {
return false;
}
}
Note: Outside the class, on my main window (wxwidgets) this is 开发者_Python百科working without any problem:
str = wxT("SELECT idclient, name, created_by, created_at, modified_by, modified_at FROM client WHERE isdel=0 ORDER BY name ASC;");
mysql_query(&mysql, str.mb_str());
result = mysql_store_result(&mysql);
Update
The goal of the class was to simply use it from the wxWidgets parts without taking care of the convertion.
I have tried to separate but it doesn't seem that the problem is coming from the wxString
.
Something really strange I haven't been able to understand is that if I call:
MySQL sql;
sql.Query(wxT("blabla"), NULL)
from the constructor of my wxFrame
it doesn't crash. If I do the same on a function on the wxFrame
class, itself called from its constructor, it crashes.
like:
constructor {
Query
}
Ok
constructor {
function()
}
fonction {
Query
}
Crash
I'm really confused...
Why are you mixing wxStrings and MySQL? That doesn't seem like a good idea. I would highly suggest just using normal strings everywhere, and separate out the WxWidgets code and the MySQL code completely, so you can figure out what's going wrong properly.
精彩评论