Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set class for database connection
QtWS25 Last Chance

Set class for database connection

Scheduled Pinned Locked Moved General and Desktop
sqlconnectionclass
4 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gillou_beginqt
    wrote on last edited by Gillou_beginqt
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Gillou_beginqt
        wrote on last edited by Gillou_beginqt
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          How are you setting up your model ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved