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. QSqlDatabase singleton class problems
Qt 6.11 is out! See what's new in the release blog

QSqlDatabase singleton class problems

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.1k 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.
  • LodiCodeL Offline
    LodiCodeL Offline
    LodiCode
    wrote on last edited by
    #1

    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 jsulmJ 2 Replies Last reply
    0
    • LodiCodeL LodiCode

      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
      
      
      K Offline
      K Offline
      Kevin Zhong
      wrote on last edited by Kevin Zhong
      #2

      @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
      0
      • LodiCodeL Offline
        LodiCodeL Offline
        LodiCode
        wrote on last edited by LodiCode
        #3

        @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
        0
        • LodiCodeL LodiCode

          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
          
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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
          2

          • Login

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