Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. database issue
Forum Updated to NodeBB v4.3 + New Features

database issue

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
20 Posts 5 Posters 1.3k Views 1 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.
  • jsulmJ jsulm

    @nikhilsarma said in database issue:

    now what i'm requesting to help me is please tell me the code which sets default location in user pc and access database,please somebody help

    Use https://doc.qt.io/qt-5/qstandardpaths.html to get the path to the location/folder where you want to store the database file.

    N Offline
    N Offline
    nikhilsarma-
    wrote on last edited by
    #7

    @jsulm sir please give me a example code,please it will be very helpfull for my life

    jsulmJ 1 Reply Last reply
    0
    • N nikhilsarma-

      @jsulm sir please give me a example code,please it will be very helpfull for my life

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

      @nikhilsarma If you open the link I gave you you will see code example. What else do you need?

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

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nikhilsarma-
        wrote on last edited by
        #9

        sir i am using like this but its opening files folder not opening database file and search data for
        user login credentials,please sir give me a sample code to open database file and search data in it,please help me sir.

        QSqlDatabase mydb;

           void connClose()//closeing the connection and also removing default connections if any database is connected.
           {
               mydb.close();
               mydb.removeDatabase(QSqlDatabase::defaultConnection);
           }
        
        
           bool connOpen()//connecting to database,cheaking through bool data type that database is connected or not.
           {
        

        // QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
        // QString dbFilePath = paths[0] + QDir::separator() + "digilogic.db";
        // mydb=QSqlDatabase::addDatabase("QSQLITE");
        // mydb.setDatabaseName(dbFilePath);

          // mydb=QSqlDatabase::addDatabase("QSQLITE");
           //mydb.setDatabaseName("/home/nikhil/digilogic.db");
        

        // QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
        // "/home/nikhil/digilogic.db",
        // tr("digilogic.db"));
        QStringList files = QFileDialog::getOpenFileNames(
        this,
        "digilogic.db",
        "/home",
        ".db");

           if(!mydb.open()){
        
              qDebug()<<("failed to open the database");
              return false;
           }
              else{
               qDebug()<<("connected...");
               return true;
                  }
        
        jsulmJ 1 Reply Last reply
        0
        • N nikhilsarma-

          sir i am using like this but its opening files folder not opening database file and search data for
          user login credentials,please sir give me a sample code to open database file and search data in it,please help me sir.

          QSqlDatabase mydb;

             void connClose()//closeing the connection and also removing default connections if any database is connected.
             {
                 mydb.close();
                 mydb.removeDatabase(QSqlDatabase::defaultConnection);
             }
          
          
             bool connOpen()//connecting to database,cheaking through bool data type that database is connected or not.
             {
          

          // QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
          // QString dbFilePath = paths[0] + QDir::separator() + "digilogic.db";
          // mydb=QSqlDatabase::addDatabase("QSQLITE");
          // mydb.setDatabaseName(dbFilePath);

            // mydb=QSqlDatabase::addDatabase("QSQLITE");
             //mydb.setDatabaseName("/home/nikhil/digilogic.db");
          

          // QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
          // "/home/nikhil/digilogic.db",
          // tr("digilogic.db"));
          QStringList files = QFileDialog::getOpenFileNames(
          this,
          "digilogic.db",
          "/home",
          ".db");

             if(!mydb.open()){
          
                qDebug()<<("failed to open the database");
                return false;
             }
                else{
                 qDebug()<<("connected...");
                 return true;
                    }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #10

          @nikhilsarma said in database issue:

          QFileDialog::getOpenFileNames

          This is not what I suggested to use. Use https://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName
          And where in your code do you set the db file in mydb?!

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

          N 1 Reply Last reply
          0
          • jsulmJ jsulm

            @nikhilsarma Lets say you want to store the database file in user documents folder (C:/Users/<USER>/Documents on Windows) then you do:

            QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
            QString dbFilePath = paths[0] + QDir::separator() + "digilogic.db";
            mydb=QSqlDatabase::addDatabase("QSQLITE");
            mydb.setDatabaseName(dbFilePath);
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #11

            @jsulm said in database issue:

            QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
            QString dbFilePath = paths[0] + QDir::separator() + "digilogic.db";

            May I suggest a more robust version:

            QString path =QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
            QString dbFilePath = path + QDir::separator() + "digilogic.db";
            

            this guarantees a path where your app can read and write to :D


            and the recommended enum to use would be QStandardPaths::AppDataLocation for read/write data only your application is intended to use


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            3
            • jsulmJ jsulm

              @nikhilsarma said in database issue:

              QFileDialog::getOpenFileNames

              This is not what I suggested to use. Use https://doc.qt.io/qt-5/qfiledialog.html#getOpenFileName
              And where in your code do you set the db file in mydb?!

              N Offline
              N Offline
              nikhilsarma-
              wrote on last edited by
              #12

              @jsulm sir,i am very new to qt ,i didnt have any training in it ,but i managed to do alot of programming in it ,but i got stuck in this paths concept please give me a solution,

              this is my code of loginwindow class where i created two functions which are used to open and close database file,and using loginwindow class object i am accessing both open and close functions in another windows or cpp files ,what i am requesting you to help me is without giving any path of database file i want my program to search for it in my pc as well as user pc when i give my application to user,i hope u understand my problem and give me some code,thank you very much for your help till now ,please give me some solution.

              class loginwindow : public QMainWindow
              {
              Q_OBJECT
              public:

              QSqlDatabase mydb;
              
                 void connClose()//closeing the connection and also removing default connections if any database is connected.
                 {
                     mydb.close();
                     mydb.removeDatabase(QSqlDatabase::defaultConnection);
                 }
                 bool connOpen()//connecting to database,cheaking through bool data type that database is connected or not.
                 {
              
                       mydb=QSqlDatabase::addDatabase("QSQLITE");
                       mydb.setDatabaseName("/home/nikhil/digilogic.db");
              
                 if(!mydb.open()){
              
                    qDebug()<<("failed to open the database");
                    return false;
                 }
                    else{
                     qDebug()<<("connected...");
                     return true;
                        }
                 }
              

              public:
              loginwindow(QWidget *parent = nullptr);
              ~loginwindow();

              private slots:
              void on_userlogin_clicked();

              void on_signupbutton_clicked();
              

              private:
              Ui::loginwindow *ui;
              };

              #endif // LOGINWINDOW_H

              jsulmJ 1 Reply Last reply
              0
              • N nikhilsarma-

                @jsulm sir,i am very new to qt ,i didnt have any training in it ,but i managed to do alot of programming in it ,but i got stuck in this paths concept please give me a solution,

                this is my code of loginwindow class where i created two functions which are used to open and close database file,and using loginwindow class object i am accessing both open and close functions in another windows or cpp files ,what i am requesting you to help me is without giving any path of database file i want my program to search for it in my pc as well as user pc when i give my application to user,i hope u understand my problem and give me some code,thank you very much for your help till now ,please give me some solution.

                class loginwindow : public QMainWindow
                {
                Q_OBJECT
                public:

                QSqlDatabase mydb;
                
                   void connClose()//closeing the connection and also removing default connections if any database is connected.
                   {
                       mydb.close();
                       mydb.removeDatabase(QSqlDatabase::defaultConnection);
                   }
                   bool connOpen()//connecting to database,cheaking through bool data type that database is connected or not.
                   {
                
                         mydb=QSqlDatabase::addDatabase("QSQLITE");
                         mydb.setDatabaseName("/home/nikhil/digilogic.db");
                
                   if(!mydb.open()){
                
                      qDebug()<<("failed to open the database");
                      return false;
                   }
                      else{
                       qDebug()<<("connected...");
                       return true;
                          }
                   }
                

                public:
                loginwindow(QWidget *parent = nullptr);
                ~loginwindow();

                private slots:
                void on_userlogin_clicked();

                void on_signupbutton_clicked();
                

                private:
                Ui::loginwindow *ui;
                };

                #endif // LOGINWINDOW_H

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

                @nikhilsarma said in database issue:

                without giving any path of database file i want my program to search for it in my pc as well as user pc when i give my application to user,i hope u understand my problem

                I understood your problem already before and told you what you can use to let the user search for database file.
                Now you post code without my suggestion! Why? Do you want me to write the code for you? I will do it this time, but I really hope you will start to at least try to do what others suggest...

                bool connOpen(const QString &dbFileName)
                   {
                
                         mydb=QSqlDatabase::addDatabase("QSQLITE");
                         mydb.setDatabaseName(dbFileName);
                ...
                
                // Somewhere else (I don't know where you want to do this, up to you
                QString dbFileName = QFileDialog::getOpenFileName(
                    this,
                    tr("Open File"),
                    "/home",
                    tr("Database (*.db)");
                if (!dbFileName.isNull())
                    loginWindowInstance.cannOpen(dbFileName);
                

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

                N 1 Reply Last reply
                3
                • jsulmJ jsulm

                  @nikhilsarma said in database issue:

                  without giving any path of database file i want my program to search for it in my pc as well as user pc when i give my application to user,i hope u understand my problem

                  I understood your problem already before and told you what you can use to let the user search for database file.
                  Now you post code without my suggestion! Why? Do you want me to write the code for you? I will do it this time, but I really hope you will start to at least try to do what others suggest...

                  bool connOpen(const QString &dbFileName)
                     {
                  
                           mydb=QSqlDatabase::addDatabase("QSQLITE");
                           mydb.setDatabaseName(dbFileName);
                  ...
                  
                  // Somewhere else (I don't know where you want to do this, up to you
                  QString dbFileName = QFileDialog::getOpenFileName(
                      this,
                      tr("Open File"),
                      "/home",
                      tr("Database (*.db)");
                  if (!dbFileName.isNull())
                      loginWindowInstance.cannOpen(dbFileName);
                  
                  N Offline
                  N Offline
                  nikhilsarma-
                  wrote on last edited by nikhilsarma-
                  #14

                  @jsulm i still did'nt get how to use the code (// Somewhere else (I don't know where you want to do this, up to you) this part is confuseing me ,but thank you sir for showing me some path.

                  JonBJ 1 Reply Last reply
                  0
                  • N nikhilsarma-

                    @jsulm i still did'nt get how to use the code (// Somewhere else (I don't know where you want to do this, up to you) this part is confuseing me ,but thank you sir for showing me some path.

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

                    @nikhilsarma said in database issue:

                    // Somewhere else (I don't know where you want to do this, up to you) this part is confuseing me

                    I cannot see what can be confusing. Put the code @jsulm gave you to open the database wherever it is that ... you want to open the database.

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      nikhilsarma-
                      wrote on last edited by nikhilsarma-
                      #16

                      @jsulm
                      @JonB

                      Thank you sir,for your response ,what i am trying to do is ,i created a loginwindow class where i created two functions which are used to open and close database file,and using loginwindow class object i am accessing both open and close functions in another windows or cpp files those are funtions are connOpen and connClose ,

                      class loginwindow : public QMainWindow
                      {
                      Q_OBJECT
                      public:

                      QSqlDatabase mydb;
                      
                      QString dbFileName = QFileDialog::getOpenFileName(
                          this,
                          tr("Open File"),
                          "/home",
                          tr("Database (*.db)"));   
                      
                      bool connOpen(const QString &dbFileName)
                         {
                      
                               mydb=QSqlDatabase::addDatabase("QSQLITE");
                               mydb.setDatabaseName(dbFileName);
                      
                               if (!dbFileName.isNull())
                               loginwindowInstance.connOpen(dbFileName);// i am getting error as (use of underdeclared identifier loginwindowinstance )
                      
                         if(!mydb.open()){
                      
                            qDebug()<<("failed to open the database");
                            return false;
                         }
                            else{
                             qDebug()<<("connected...");
                             return true;
                                }
                         }
                      
                         void connClose()//closeing the connection and also removing default connections if any database is connected.
                         {
                             mydb.close();
                             mydb.removeDatabase(QSqlDatabase::defaultConnection);
                         }
                      

                      so if i wanted to use those functions in another cpp files i am doing like this

                      void employeeinfo::on_delete_2_clicked()
                      
                      {   loginwindow conn;
                      
                         username=ui->Username_Employinfo->text();
                         if(!conn.connOpen()){
                             qDebug()<<"Failed to open the database";
                             return;
                         }
                         conn.connOpen();//this function will open your connection
                         QSqlQuery qry;
                         qry.prepare("Delete from employinfo where username='"+username+"'");
                      
                         if(qry.exec( ))
                            {
                               QMessageBox::information(this,tr("Delete"),tr("Deleted"));
                               conn.connClose();
                             }
                         else
                         {
                             QMessageBox::critical(this,tr("error::"),qry.lastError().text());
                         }
                      

                      }

                      Creating object of loginwindow class and using to open database,i want to open and close database only through loginwindow class ,so that i will create object of that class and use in other files.

                      i hope u understand my problem sir.

                      jsulmJ 1 Reply Last reply
                      0
                      • N nikhilsarma-

                        @jsulm
                        @JonB

                        Thank you sir,for your response ,what i am trying to do is ,i created a loginwindow class where i created two functions which are used to open and close database file,and using loginwindow class object i am accessing both open and close functions in another windows or cpp files those are funtions are connOpen and connClose ,

                        class loginwindow : public QMainWindow
                        {
                        Q_OBJECT
                        public:

                        QSqlDatabase mydb;
                        
                        QString dbFileName = QFileDialog::getOpenFileName(
                            this,
                            tr("Open File"),
                            "/home",
                            tr("Database (*.db)"));   
                        
                        bool connOpen(const QString &dbFileName)
                           {
                        
                                 mydb=QSqlDatabase::addDatabase("QSQLITE");
                                 mydb.setDatabaseName(dbFileName);
                        
                                 if (!dbFileName.isNull())
                                 loginwindowInstance.connOpen(dbFileName);// i am getting error as (use of underdeclared identifier loginwindowinstance )
                        
                           if(!mydb.open()){
                        
                              qDebug()<<("failed to open the database");
                              return false;
                           }
                              else{
                               qDebug()<<("connected...");
                               return true;
                                  }
                           }
                        
                           void connClose()//closeing the connection and also removing default connections if any database is connected.
                           {
                               mydb.close();
                               mydb.removeDatabase(QSqlDatabase::defaultConnection);
                           }
                        

                        so if i wanted to use those functions in another cpp files i am doing like this

                        void employeeinfo::on_delete_2_clicked()
                        
                        {   loginwindow conn;
                        
                           username=ui->Username_Employinfo->text();
                           if(!conn.connOpen()){
                               qDebug()<<"Failed to open the database";
                               return;
                           }
                           conn.connOpen();//this function will open your connection
                           QSqlQuery qry;
                           qry.prepare("Delete from employinfo where username='"+username+"'");
                        
                           if(qry.exec( ))
                              {
                                 QMessageBox::information(this,tr("Delete"),tr("Deleted"));
                                 conn.connClose();
                               }
                           else
                           {
                               QMessageBox::critical(this,tr("error::"),qry.lastError().text());
                           }
                        

                        }

                        Creating object of loginwindow class and using to open database,i want to open and close database only through loginwindow class ,so that i will create object of that class and use in other files.

                        i hope u understand my problem sir.

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

                        @nikhilsarma said in database issue:

                        class loginwindow : public QMainWindow
                        {
                        Q_OBJECT
                        public:
                        QSqlDatabase mydb;

                        QString dbFileName = QFileDialog::getOpenFileName(
                        this,
                        tr("Open File"),
                        "/home",
                        tr("Database (*.db)"));

                        I suggest you learn C++ basics as this code is invalid.
                        You need to use QFileDialog::getOpenFileName when you want to ask user to select the database file.
                        Since its your app I don't know when user should select the database file, this is something you have to think about...

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

                        N 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @nikhilsarma said in database issue:

                          class loginwindow : public QMainWindow
                          {
                          Q_OBJECT
                          public:
                          QSqlDatabase mydb;

                          QString dbFileName = QFileDialog::getOpenFileName(
                          this,
                          tr("Open File"),
                          "/home",
                          tr("Database (*.db)"));

                          I suggest you learn C++ basics as this code is invalid.
                          You need to use QFileDialog::getOpenFileName when you want to ask user to select the database file.
                          Since its your app I don't know when user should select the database file, this is something you have to think about...

                          N Offline
                          N Offline
                          nikhilsarma-
                          wrote on last edited by
                          #18

                          @jsulm sir,I know all cpp concepts,but I think QFileDialog concept is of qt’s cpp ,which is were I am faceing difficulty to understand,

                          Sir one last help I need from you is, what all concepts I need to learn to access or configure database file’s or any kind of file’s from anywhere in my PC and in user PC also ,thanks ones again sir for replying to post.

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

                            Hi,

                            @nikhilsarma said in database issue:

                            class loginwindow : public QMainWindow
                            {
                            Q_OBJECT
                            public:
                            QSqlDatabase mydb;

                            QString dbFileName = QFileDialog::getOpenFileName(
                            this,
                            tr("Open File"),
                            "/home",
                            tr("Database (*.db)"));

                            bool connOpen(const QString &dbFileName)
                            {

                            getOpenFileName is a static method. You can't use it in such a variable declaration.

                            As @jsulm already wrote, mydb should not exist in the first place as explained in the QSqlDatabase documentation.

                            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
                            • N nikhilsarma-

                              @jsulm sir,I know all cpp concepts,but I think QFileDialog concept is of qt’s cpp ,which is were I am faceing difficulty to understand,

                              Sir one last help I need from you is, what all concepts I need to learn to access or configure database file’s or any kind of file’s from anywhere in my PC and in user PC also ,thanks ones again sir for replying to post.

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

                              @nikhilsarma said in database issue:

                              ,I know all cpp concepts

                              Then why do you write invalid code? It doesn' tmatter that QFileDialog is a class from Qt, it could be any class.
                              This code is invalid because you most certainly do not want to show the file dialog when you create an instance of that class:

                              class loginwindow : public QMainWindow
                              {
                              Q_OBJECT
                              public:
                              QSqlDatabase mydb;
                              
                              // Bellow line is invalid
                              QString dbFileName = QFileDialog::getOpenFileName(
                              this,
                              tr("Open File"),
                              "/home",
                              tr("Database (*.db)"));
                              

                              And did you actually think about what you want to do? Don't you think you should call QFileDialog::getOpenFileName when you want to ask user to select the db file?

                              And I repeat it once more: do NOT store database connections in member variables! mydb member is not needed...

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

                              1 Reply Last reply
                              4

                              • Login

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