简体   繁体   English

使用带有wxWidgets的mysql lib的类崩溃

[英]Crash using a class using mysql lib with wxWidgets

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. 我写了一个小类在项目中使用c mysql lib。

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. 也许某些高级的ppl会知道。

Sql class: SQL类:

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: SQL函数:

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 working without any problem: 注意:在课外,在我的主窗口(wxwidgets)上,它没有任何问题:

 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. 该类的目标是简单地从wxWidgets部分使用它,而无需进行转换。

I have tried to separate but it doesn't seem that the problem is coming from the wxString . 我试图分开,但是似乎问题出在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. 从我的wxFrame的构造函数不会崩溃。 If I do the same on a function on the wxFrame class, itself called from its constructor, it crashes. 如果我对wxFrame类的一个函数(从其构造函数本身调用)执行相同的操作,则会崩溃。

like: 喜欢:

constructor {
   Query
}

Ok

constructor {
 function()
}

fonction {
    Query
}

Crash 崩溃

I'm really confused... 我真的很困惑

Why are you mixing wxStrings and MySQL? 为什么要混合wxStrings和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. 我强烈建议仅在任何地方使用普通字符串,并将WxWidgets代码和MySQL代码完全分开,这样您就可以正确地找出问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM