Problem : MySQL server has gone away QMYSQL: Unable to execute query
-
wrote on 12 Jun 2013, 17:38 last edited by
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("insert into outbox (number,text) 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("insert into outbox (number,text) 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.
-
wrote on 12 Jun 2013, 22:16 last edited by
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()@ -
wrote on 19 Jan 2014, 12:12 last edited by
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();
}
@