Important: Please read the Qt Code of Conduct -

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

  • 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
    cout<<"Connexion à la base de donnée locale échouée.";
    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;
    QFile file("test.txt"); | QIODevice::Text);
    QTextStream flux(&file);

            ok=query.exec&#40;"insert into outbox (number,text&#41; values('"+numero+"','"+message.toUtf8()+"');");  
            flux.operator <<(query.lastError().text()+"\n");  
                string mess=message.toStdString().substr(i*160,160);  
                ostringstream oss;  
                query.exec&#40;"insert into outbox (number,text&#41; values('"+numero+"','"+message.toUtf8()+"');");  
    }  @

    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.

  • 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:


  • Is it possible to use reconnect option to prevent error on timeout?
    Like this:
    @ db = QSqlDatabase::addDatabase("QMYSQL");
    // ...
    qDebug() << "Error connecting to DB: " << db.lastError().text();
    return db.lastError();

Log in to reply