Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem : MySQL server has gone away QMYSQL: Unable to execute query
Forum Updated to NodeBB v4.3 + New Features

Problem : MySQL server has gone away QMYSQL: Unable to execute query

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 12.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pepitoty
    wrote on 12 Jun 2013, 17:38 last edited by
    #1

    Hello, I ask this message because I have a problem with QMySql. I create a local database consists of two tables (inbox, outbox), I use smsd daemon to be able to send messages via Mobile module. I use a thread to regularly go retrieve incoming messages (inbox table) and send messages that I insert in the other table (outbox). I develop on OpenSuse 12.2 and Qt 4.8.1 operating system. I use QMYSQL driver and my problem is that I can only insert in the outbox table once. I let you see the code:

    My first window opens the database:

    @ db=new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL","qmysql"));
    db->setUserName("root"); //-Connexion à la base de donnée locale
    db->setDatabaseName("smsd");
    db->setHostName("127.0.0.1");
    db->setPort(3306);
    if(!db->open())
    {
    cout<<"Connexion à la base de donnée locale échouée.";
    }
    else
    {
    this->sms = new C_Gsm(db);
    cout<<"Connexion à la base de donnée locale réussie.";
    sms->LancerThread(); //-Lancement du thread du gsm
    App = new C_Application_Chauffeur(sms);
    this->identifiant = App->DemanderPlanning();
    ui->label_5->setText(identifiant); //-Affichage de l'identifiant de connexion OpenSuse
    } @

    My method that fits into the outbox table:

    @ QSqlQuery query(*db);
    bool ok=0;
    short nbsms=(message.length()/160)+1,i=0;
    if(nbsms==1)
    {
    QFile file("test.txt");
    file.open(QIODevice::Append | QIODevice::Text);
    QTextStream flux(&file);
    flux.setCodec("UTF-8");

            ok=query.exec&#40;"insert into outbox (number,text&#41; values('"+numero+"','"+message.toUtf8()+"');");  
      
            flux.operator <<(query.lastError().text()+"\n");  
            file.close();  
        }  
        else  
        {  
            do  
            {  
                string mess=message.toStdString().substr(i*160,160);  
                ostringstream oss;  
                oss<<nbsms<<"-"<<i+1;  
                mess.replace(4,3,oss.str());  
                query.exec&#40;"insert into outbox (number,text&#41; values('"+numero+"','"+message.toUtf8()+"');");  
                i++;  
            }while((i+1)<=nbsms);  
        }  
        message.clear();  
    }  @
    

    As you can see, I create a text file that shows me the sql error log, the error message is:

    MySQL server has gone away QMYSQL: Unable to execute query

    I do not understand really, I did some research on the internet but I have not found... The thing is that I can fit into my table again, but next time, the error message!

    Thank you in advance and sorry for my bad English.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      francescmm
      wrote on 12 Jun 2013, 22:16 last edited by
      #2

      Could you show us the error that MySQL reports?

      @int error = query.lastError().number();@

      It could be possible that if the error is 2006 or 2013, the problem is that MySQL close the socket connection due a internal time out in the server configuration.

      Two methods to change it are: to disable the timeout on the server or disconnect and reconnect to MySQL with:

      @db->close();
      db->open()@

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stoyanps
        wrote on 19 Jan 2014, 12:12 last edited by
        #3

        Is it possible to use reconnect option to prevent error on timeout?
        Like this:
        @ db = QSqlDatabase::addDatabase("QMYSQL");
        // ...
        db.setConnectOptions("MYSQL_OPT_RECONNECT=1");
        if(!db.open())
        {
        qDebug() << "Error connecting to DB: " << db.lastError().text();
        return db.lastError();
        }
        @

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved