c++sqlqtqsqldatabasechange-notification

Is there any qt signal for sql database change?


I wrote a C++ program using Qt. some variables inside my algorithm are changed outside of my program and in a web page. every time the user changes the variable values in the web page I modify a pre-created SQL database. Now I want my code to change the variables value during run time without to stop the code. there is two options :

  1. Every n seconds check the database and retrieve the variables value -> this is not good since I have to check if database content is changed every n seconds (it might be without any change for years. Also I don't want to check if the database content is changed)

  2. Every time the database is changed my Qt program emits a signal so by catching this signal I can refresh the variables value, This seems an optimal solution and I want to write a code for this part

The C++ part of my code is:

void Update Database()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("Mydataset");

    db.setUserName("user");
    db.setPassword("pass");

    if(!db.open())
    {
        qDebug()<<"Error is: "<<db.lastError();
        qFatal("Failed To Connect");
    }
    QSqlQuery qry;
    qry.exec("SELECT * from tblsystemoptions");
    QSqlRecord rec = qry.record();
    int cols = rec.count();
    qry.next();
    MCH = qry.value(0).toString();  //some global variables used in other functions
    MCh = qry.value(1).toString();
    // ... this goes on ...


}

Solution

  • There is no such thing. The Qt event loop and the database are not connected in any way. You only fetch/alter/delete/insert/... data and that's it. Option 1 is the one you have to do. There are ways to use TRIGGER on the server side to launch external scripts but this would not help you very much.