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. initialize connection database in .h
Forum Updated to NodeBB v4.3 + New Features

initialize connection database in .h

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 220 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.
  • C Offline
    C Offline
    Cervo2paille
    wrote on last edited by
    #1

    Hello,

    I'm tring to externalize my database connection.
    This code works :
    mainwindow.cpp

              database.setHostName("localhost");
               database.setDatabaseName("base");
               database.setUserName("base");
               database.setPassword("base");
    
          QString identifiant = ui->identifiant->text();
          QString motdepasse = ui->motdepasse->text();
    
          QSqlQuery qry;
          qry.prepare("INSERT INTO utilisateur (identifiant, motdepasse)"
                      "VALUES (:identifiant,:motdepasse)");
          qry.bindValue(":identifiant", identifiant);
          qry.bindValue(":motdepasse", motdepasse);
    
          if(qry.exec()) {
                QMessageBox::information(this,"Inséré","Vous êtes bien inscrits");
          } else{
                QMessageBox::information(this,"Pas inséré","Les données ne se sont pas bien insérées");
          }
    }
    

    Now i want to put my connection in another file and call it.
    So i changed to this :
    mainwindow.cpp

    #include "initdb.h"
    #include <QtSql>
    
            if (!QSqlDatabase::drivers().contains("QMYSQL"))
                QMessageBox::critical(
                            this,
                            "Unable to load database",
                            "This demo needs the QMYSQL driver"
                            );
    
            // Initialize the database:
            QSqlError err = initDb();
            if (err.type() != QSqlError::NoError) {
                showError(err);
                return;
            }
    
          QString identifiant = ui->identifiant->text();
          QString motdepasse = ui->motdepasse->text();
    
          QSqlQuery qry;
          qry.prepare("INSERT INTO utilisateur (identifiant, motdepasse)"
                      "VALUES (:identifiant,:motdepasse)");
          qry.bindValue(":identifiant", identifiant);
          qry.bindValue(":motdepasse", motdepasse);
    
          if(qry.exec()) {
                QMessageBox::information(this,"Inséré","Vous êtes bien inscrits");
          } else{
                QMessageBox::information(this,"Pas inséré","Les données ne se sont pas bien insérées");
          }
    }
    

    initdb.h

    #ifndef INITDB_H
    #define INITDB_H
    
    #include <QtSql>
    
    QSqlError initDb()
    {
        QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL");
        database.setHostName("base");
        database.setDatabaseName("base");
        database.setUserName("base");
        database.setPassword("base");
    
        if (!database.open())
            return database.lastError();
    
        return QSqlError();
    }
    
    #endif // INITDB_H
    
    

    But the insertion doesn't work. No error, but it seems to not be initalize.

    Thanks

    jsulmJ 1 Reply Last reply
    0
    • C Cervo2paille

      Hello,

      I'm tring to externalize my database connection.
      This code works :
      mainwindow.cpp

                database.setHostName("localhost");
                 database.setDatabaseName("base");
                 database.setUserName("base");
                 database.setPassword("base");
      
            QString identifiant = ui->identifiant->text();
            QString motdepasse = ui->motdepasse->text();
      
            QSqlQuery qry;
            qry.prepare("INSERT INTO utilisateur (identifiant, motdepasse)"
                        "VALUES (:identifiant,:motdepasse)");
            qry.bindValue(":identifiant", identifiant);
            qry.bindValue(":motdepasse", motdepasse);
      
            if(qry.exec()) {
                  QMessageBox::information(this,"Inséré","Vous êtes bien inscrits");
            } else{
                  QMessageBox::information(this,"Pas inséré","Les données ne se sont pas bien insérées");
            }
      }
      

      Now i want to put my connection in another file and call it.
      So i changed to this :
      mainwindow.cpp

      #include "initdb.h"
      #include <QtSql>
      
              if (!QSqlDatabase::drivers().contains("QMYSQL"))
                  QMessageBox::critical(
                              this,
                              "Unable to load database",
                              "This demo needs the QMYSQL driver"
                              );
      
              // Initialize the database:
              QSqlError err = initDb();
              if (err.type() != QSqlError::NoError) {
                  showError(err);
                  return;
              }
      
            QString identifiant = ui->identifiant->text();
            QString motdepasse = ui->motdepasse->text();
      
            QSqlQuery qry;
            qry.prepare("INSERT INTO utilisateur (identifiant, motdepasse)"
                        "VALUES (:identifiant,:motdepasse)");
            qry.bindValue(":identifiant", identifiant);
            qry.bindValue(":motdepasse", motdepasse);
      
            if(qry.exec()) {
                  QMessageBox::information(this,"Inséré","Vous êtes bien inscrits");
            } else{
                  QMessageBox::information(this,"Pas inséré","Les données ne se sont pas bien insérées");
            }
      }
      

      initdb.h

      #ifndef INITDB_H
      #define INITDB_H
      
      #include <QtSql>
      
      QSqlError initDb()
      {
          QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL");
          database.setHostName("base");
          database.setDatabaseName("base");
          database.setUserName("base");
          database.setPassword("base");
      
          if (!database.open())
              return database.lastError();
      
          return QSqlError();
      }
      
      #endif // INITDB_H
      
      

      But the insertion doesn't work. No error, but it seems to not be initalize.

      Thanks

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Cervo2paille said in initialize connection database in .h:

      QSqlQuery qry;

      You need to tell the query which database to use as you do not use the default one:

      QSqlQuery qry(QSqlDatabase::database("base"));
      

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

      JonBJ 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Cervo2paille said in initialize connection database in .h:

        QSqlQuery qry;

        You need to tell the query which database to use as you do not use the default one:

        QSqlQuery qry(QSqlDatabase::database("base"));
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @jsulm said in initialize connection database in .h:

        You need to tell the query which database to use as you do not use the default one:

        ? Where does code set a connection name? I can only see setDatabaseName("base"), that is database not connection name?

        @Cervo2paille
        I do not know what you are trying to do. Why are you trying to write database opening code in a .h file? And you only want to open the database once in any circumstance. After that you can access it via static QSqlDatabase QSqlDatabase::database(), if you need it all. If you leave it as you have as the default connection you won't need it anyway.

        1 Reply Last reply
        1

        • Login

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