Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Notice Close of MySql Server in Qt

When I closed MySql server, how can I understand that mysql server is gone away from my Qt program?

Edit:

Here my trial:

When I close MySql, I get these results, and I can't catch that MySql is closed.

My Code Snippet is

QSqlQuery query(db);
query.exec("SELECT * From RequestIds");
qDebug()<<query.lastError();
qDebug()<<db.lastError()<<QTime::currentTime();
qDebug()<<db.isOpen();
qDebug()<<db.isValid();

and output is:

QSqlError(2006, "QMYSQL: Unable to execute query", "MySQL server has gone away") 
QSqlError(-1, "", "") QTime("14:22:58") 
true 
true

I don't understand why db.isOpen() returns true.

like image 831
metdos Avatar asked Oct 14 '10 13:10

metdos


People also ask

What is QSqlQuery?

QSqlQuery encapsulates the functionality involved in creating, navigating and retrieving data from SQL queries which are executed on a QSqlDatabase.


2 Answers

There is a bug related with QSqlDatabase::isOpen() in Qt.

https://bugreports.qt.io/browse/QTBUG-223

like image 144
metdos Avatar answered Oct 03 '22 02:10

metdos


Your program has no idea of its surroundings. If something changes, you may be able to have the OS notify your program, or you'll have to test yourself.

If the database connection closes before your program, the status from the connection should return some kind of error code. You are checking status from the connection functions?

Write a simple program that opens a window and upon the click of a button, writes to the database. After writing to the database, the program should display the status in the window. Run your program. Press button to get the "controlled" response. Close the database then click on the button again.

You may be able to do this with a debugger, depending on the ability of the debugger & OS to queue up messages.

like image 45
Thomas Matthews Avatar answered Oct 03 '22 01:10

Thomas Matthews