Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved QSqlDatabase singleton class problems

    General and Desktop
    3
    4
    1090
    Loading More Posts
    • 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.
    • LodiCode
      LodiCode last edited by

      hello guys,

      I am thinking how is the best way to made a program using QSql, how to organize it most clear possible, i made this class, but it didn't load mysql driver, where i made a mistake, if you know a best way to do this, help me, thanks alot!

      #ifndef DATABASE_H
      #define DATABASE_H
      
      #include <QSqlDatabase>
      #include <QDebug>
      
      class Connection: public QSqlDatabase{
          private:
              Connection(){}
      
          public:
              static Connection &getInstance(){
                  static Connection conn;
                  if(!conn.contains("sisdeposito")){
                      conn = Connection();
                      conn.addDatabase("QMYSQL");
                      conn.setHostName("127.0.0.1");
                      conn.setPort(3306);
                      conn.setDatabaseName("sisdeposito");
                      conn.setUserName("root");
                      conn.setPassword("aaxd31");
                      if(conn.open())
                          qDebug() << "Conectado";
                  }
                  return conn;
              }
      };
      
      #endif // DATABASE_H
      
      

      Let's code?

      K jsulm 2 Replies Last reply Reply Quote 0
      • K
        Kevin Zhong @LodiCode last edited by Kevin Zhong

        @LodiCode
        I'm not sure if you had add sql support in .pro files

        Second, Have you compiled your database driver for MySQL?

        1 Reply Last reply Reply Quote 0
        • LodiCode
          LodiCode last edited by LodiCode

          @Kevin-Zhong

          yes i add it, my problem is class Connection : public QSqlDatabase,

          i set static Connection conn;
          and conn.addDatabase("QMYSQL");

          but if i change all Connections in getInstance for QSqlDatabase, it work, but it is correct? have other way to do it?

          thanks for you time.

          Let's code?

          1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @LodiCode last edited by

            @LodiCode Why do you need such a class? QSqlDatabase can already manage as many connections as you need, see here http://doc.qt.io/qt-5/qsqldatabase.html. You can use the default connection or give your connections names and access them by names.
            Regarding MySQL driver: what does http://doc.qt.io/qt-5/qsqldatabase.html#drivers return? How did you install Qt and which version?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 2
            • First post
              Last post