Set class for database connection



  • HI I am a beginner in Gui development using qt.
    I have a project where I have to use a mysql database. I want to know how to create a class for my connection to the database and for the query so It will be easy for me to call this class in the different windows and execute sql queries


  • Lifetime Qt Champion

    Hi,

    Did you take a look at the QtSql module documentation and examples ?



  • Yeah I did. I created an header file called connection for the connection to the database as follows
    @@
    #include <QtSql>
    #include <QSql>

    static bool connOpen()
    {

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("calendar");
    db.setUserName("root");
    db.setPassword("");
    if(!db.open())
    {
    qDebug()<<("Failed to open the database");
    return false;
    }
    else
    {

      qDebug()<<("Connected");
      return true;
    

    }

    }

    @@
    I am able to add in my database but when I trying to display in a dialog windows using a table view, nothings shows in my table view


  • Lifetime Qt Champion

    How are you setting up your model ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.