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

QSqlDatabase singleton class problems

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.3k 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.
  • L Offline
    L Offline
    LodiCode
    wrote on 14 Nov 2016, 23:06 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 J 2 Replies Last reply 15 Nov 2016, 01:26
    0
    • L LodiCode
      14 Nov 2016, 23:06

      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 15 Nov 2016, 01:26 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
      • L Offline
        L Offline
        LodiCode
        wrote on 15 Nov 2016, 01:44 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
        • L LodiCode
          14 Nov 2016, 23:06

          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
          
          
          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 15 Nov 2016, 05:30 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

          1/4

          14 Nov 2016, 23:06

          • Login

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