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. Database result with connection name in all project
Qt 6.11 is out! See what's new in the release blog

Database result with connection name in all project

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.8k Views 2 Watching
  • 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.
  • elicatE Offline
    elicatE Offline
    elicat
    wrote on last edited by
    #1

    Hello, my problem of day is: how use in all class of my project one ore more database? I have try with setting connection name.

    In my project I have a class with name "DataBase" that opens a database giving a name to the connection like that:

    db = QSqlDatabase::addDatabase(driver_db, "pippo");
    

    In the same project in several parts I have to use the same database. I thought about connecting to the database by calling the connection with its name

    DataBase *database = new DataBase;
    QSqlDatabase dbstructure = QSqlDatabase::database("pippo");
    Although the connection is open and I do not have any error reports when I try to read a value of a selected record I do not return anything to the query.size () indicates me -1
            if(database->openDataBase(dbstructure)) {
            QSqlQuery querydb(dbstructure);
            QString strqrysearchprogram = "select * from nome_Tabella where Item  = '1'" ;
            if(querydb.exec(strqrysearchprogram)) {
                if (querygrare.first()) {
                    qDebug() << querygrare.size();
                    QString valuefield = querygrare.value("nameField").toString();
                    qDebug() << "valuefield : " << valuefield;
                    qDebug() << "qry: " << strqrysearchprogram;
                }
            } else {
                qDebug() << "record non trovato in qry: " << strqrysearchprogram;
            }
        } else {
            qDebug() << "ERROR open Database";
        }
    
    

    Where am I wrong? Is it correct to recall the database with the name of the connection? I state that I have checked the selection string that is passed and the record exists.

    Saluti, Gianfranco Elicat

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

      Can you show your complete code ?

      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
      • elicatE Offline
        elicatE Offline
        elicat
        wrote on last edited by elicat
        #3

        Yes, this is method for connection:

         QSqlDatabase DataBase::connectToDataBase(int type_db, QString path_and_name_db, QString driver_db, QString user_db, QString pass_db, QString hostname_db, QString port_db, QString conn_db, QString conn_name ) {
            db = QSqlDatabase::addDatabase(driver_db, conn_name);
            db.setUserName(user_db);
            db.setPassword(pass_db);
            db.setHostName(hostname_db);
            if (user_db != NULL) {
                db.setUserName(user_db);
            }
            if (pass_db != NULL) {
                db.setPassword(pass_db);
            }
            if (port_db != NULL) {
                //db.setPort(port_db);
            }
            if (conn_db != NULL) {
                db.setConnectOptions(conn_db);
            }
            switch (type_db)
            {
                case 0:
                    // accesso a database QSLite
                    db.setDatabaseName(path_and_name_db);
                break;
        
                case 1:
                    // accesso a database MSACCESS .mdb
                    db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=" + path_and_name_db);
                break;
        
                case 2:
                    // accesso a database MSACCESS .accdb
                    db.setDatabaseName("DRIVER=Provider=Microsoft.ACE.OLEDB.12.0;FIL={MS Access};DBQ=" + path_and_name_db);
                break;
        
                default:
                    // accesso a database MSACCESS .mdb
                    db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=" + path_and_name_db);
                break;
            }
            return db;
        }
        

        This is methode for open:

         bool DataBase::openDataBase(QSqlDatabase dbcheckopen) {
            if (dbcheckopen.isOpen()) {
                // first controll if is open already
                return true;
            } else {
                if(dbcheckopen.open()){
                    return true;
                } else {
                    return false;
                }
            }
        }
        

        Do you need other?

        Saluti, Gianfranco Elicat

        JonBJ 1 Reply Last reply
        0
        • elicatE elicat

          Yes, this is method for connection:

           QSqlDatabase DataBase::connectToDataBase(int type_db, QString path_and_name_db, QString driver_db, QString user_db, QString pass_db, QString hostname_db, QString port_db, QString conn_db, QString conn_name ) {
              db = QSqlDatabase::addDatabase(driver_db, conn_name);
              db.setUserName(user_db);
              db.setPassword(pass_db);
              db.setHostName(hostname_db);
              if (user_db != NULL) {
                  db.setUserName(user_db);
              }
              if (pass_db != NULL) {
                  db.setPassword(pass_db);
              }
              if (port_db != NULL) {
                  //db.setPort(port_db);
              }
              if (conn_db != NULL) {
                  db.setConnectOptions(conn_db);
              }
              switch (type_db)
              {
                  case 0:
                      // accesso a database QSLite
                      db.setDatabaseName(path_and_name_db);
                  break;
          
                  case 1:
                      // accesso a database MSACCESS .mdb
                      db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=" + path_and_name_db);
                  break;
          
                  case 2:
                      // accesso a database MSACCESS .accdb
                      db.setDatabaseName("DRIVER=Provider=Microsoft.ACE.OLEDB.12.0;FIL={MS Access};DBQ=" + path_and_name_db);
                  break;
          
                  default:
                      // accesso a database MSACCESS .mdb
                      db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=" + path_and_name_db);
                  break;
              }
              return db;
          }
          

          This is methode for open:

           bool DataBase::openDataBase(QSqlDatabase dbcheckopen) {
              if (dbcheckopen.isOpen()) {
                  // first controll if is open already
                  return true;
              } else {
                  if(dbcheckopen.open()){
                      return true;
                  } else {
                      return false;
                  }
              }
          }
          

          Do you need other?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @elicat
          I could be wrong(! @SGaist ?), but you are passing QSqlDatabase dbcheckopen to your openDataBase function. That takes a copy of the parameter, and opens that. Shouldn't it be a QSqlDatabase& dbcheckopen?

          elicatE 1 Reply Last reply
          0
          • JonBJ JonB

            @elicat
            I could be wrong(! @SGaist ?), but you are passing QSqlDatabase dbcheckopen to your openDataBase function. That takes a copy of the parameter, and opens that. Shouldn't it be a QSqlDatabase& dbcheckopen?

            elicatE Offline
            elicatE Offline
            elicat
            wrote on last edited by
            #5

            @JonB said in Database result with connection name in all project:

            I could be wron

            for gestion of memory is sure is better but in function open i only check

            Saluti, Gianfranco Elicat

            JonBJ 1 Reply Last reply
            0
            • elicatE elicat

              @JonB said in Database result with connection name in all project:

              I could be wron

              for gestion of memory is sure is better but in function open i only check

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @elicat said in Database result with connection name in all project:

              for gestion of memory is sure is better but in function open i only check

              Pardon?

              If you mean you only check whether it's already open, you don't, you call dbcheckopen.open() if it is not, it's your code....

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MrShawn
                wrote on last edited by
                #7
                QSqlDatabase dbstructure = QSqlDatabase::database("pippo");
                Although the connection is open and I do not have any error reports when I try to read a value of a selected record I do not return anything to the query.size () indicates me -1
                        if(database->openDataBase(dbstructure)) {
                                  .....
                        }
                

                try this instead:

                QSqlDatabase dbstructure = QSqlDatabase::database("pippo");
                
                        if(dbstructure.open()) {
                                  .....
                       }
                

                also include <QSqlError>
                and output

                qDebug() << querydb.lastError().text();
                //and
                qDebug() << dbstructure.lastError().text();
                

                on your failures to get more insight.

                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