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 error
Forum Updated to NodeBB v4.3 + New Features

database error

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 6 Posters 1.1k 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.
  • F fkaraokur
    28 Oct 2020, 07:41

    There is a database named "db.db" in the project folder. Even if I delete the database or change its name in my code, it does not give an error and it does not connect to the database. But it always works connected.

    QString path = "db.db";
        QSqlDatabase db;
        db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(path);
        db.open();
        if(db.isOpenError()){   qDebug()<<"Error";         }
        else {  qDebug()<<"Connected.";                        }  //always work
    
    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 28 Oct 2020, 07:49 last edited by
    #3

    @fkaraokur said in database error:

    "db.db"

    This is relative path and you wrote that it is in your project folder. But your app is looking in its current working directory which is the build folder by default, so it can't find it. Deploy the database file to the build folder. Use https://doc.qt.io/qt-5/qstandardpaths.html if you deploy your application to find the database file. Also if you need to write that file you should put it in a writable location.

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

    F 1 Reply Last reply 28 Oct 2020, 15:21
    3
    • B Offline
      B Offline
      Bonnie
      wrote on 28 Oct 2020, 08:04 last edited by
      #4

      @JonB You're right.
      Quoted from the doc:

      For the QSQLITE driver, if the database name specified does not exist, then it will create the file for you unless the QSQLITE_OPEN_READONLY option is set.

      1 Reply Last reply
      2
      • J jsulm
        28 Oct 2020, 07:49

        @fkaraokur said in database error:

        "db.db"

        This is relative path and you wrote that it is in your project folder. But your app is looking in its current working directory which is the build folder by default, so it can't find it. Deploy the database file to the build folder. Use https://doc.qt.io/qt-5/qstandardpaths.html if you deploy your application to find the database file. Also if you need to write that file you should put it in a writable location.

        F Offline
        F Offline
        fkaraokur
        wrote on 28 Oct 2020, 15:21 last edited by fkaraokur
        #5

        @jsulm said in database error:

        @fkaraokur said in database error:

        "db.db"

        This is relative path and you wrote that it is in your project folder. But your app is looking in its current working directory which is the build folder by default, so it can't find it. Deploy the database file to the build folder. Use https://doc.qt.io/qt-5/qstandardpaths.html if you deploy your application to find the database file. Also if you need to write that file you should put it in a writable location.

        When I put the db.db file in the compilation folder, it will run, right? So which is this folder? Isn't it correct if I specify the folder where the database file is located like this?

         QString path = "D:/Qt_Project/BIST/db.db";
            QSqlDatabase db;
            db = QSqlDatabase::addDatabase("QSQLITE");
            db.setDatabaseName(path);
        
        J J 2 Replies Last reply 28 Oct 2020, 15:26
        0
        • F fkaraokur
          28 Oct 2020, 15:21

          @jsulm said in database error:

          @fkaraokur said in database error:

          "db.db"

          This is relative path and you wrote that it is in your project folder. But your app is looking in its current working directory which is the build folder by default, so it can't find it. Deploy the database file to the build folder. Use https://doc.qt.io/qt-5/qstandardpaths.html if you deploy your application to find the database file. Also if you need to write that file you should put it in a writable location.

          When I put the db.db file in the compilation folder, it will run, right? So which is this folder? Isn't it correct if I specify the folder where the database file is located like this?

           QString path = "D:/Qt_Project/BIST/db.db";
              QSqlDatabase db;
              db = QSqlDatabase::addDatabase("QSQLITE");
              db.setDatabaseName(path);
          
          J Offline
          J Offline
          JonB
          wrote on 28 Oct 2020, 15:26 last edited by JonB
          #6

          @fkaraokur
          The "compilation folder" is purely an artefact of, well, compiling :) It does not have any runtime equivalent. In itself you'd have to hard-code that.

          But this is not the right place anyway to put a database which will be accessible at runtime, when your code is distributed. You probably want one of the "runtime user writeable" locations offered via the link @jsulm provided you with earlier.

          If you are saying in your situation that D:/Qt_Project/BIST/db.db is where the file sits yet the code you show fails to find/open it, then something sounds wrong.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Driftwood
            wrote on 28 Oct 2020, 16:29 last edited by Driftwood
            #7

            @fkaraokur - I'm not sure where you are with this, but I'll show you what works for me. From mainwidow.cpp

            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
                swDB = QSqlDatabase::addDatabase("QSQLITE");
                QDir dbDir;
                dbDir.mkdir("db");
                swDB.setDatabaseName("db/data.db");
            
                if (!swDB.open())
                    ui->statusBar->showMessage(swDB.lastError().text());
                else
                {
                    /*
                     * THE FOLLOWING ENSURES FOREIGN KEYS WILL BE ENABLED
                     * FOR SQLITE3. THIS MUST BE THE FIRST QUERY EXECUTED
                     * WHEN THE APP STARTS.
                     */
                    QSqlQuery q(swDB);
                    q.exec("PRAGMA foreign_keys = 1");
                    qDebug() << "Foreign Keys have been enabled";
            
                    ui->statusBar->showMessage("Database connected");
                }
                swDB.close();
            }
            

            And in mainwindow.h put this:

            private:
                QSqlDatabase swDB;
            

            The code works flawlessly. However, if you're concerns are based on the database being created after you delete it, that's normal. If the db isn't present when you run your code, Qt creates it. Every time.

            S 1 Reply Last reply 28 Oct 2020, 19:40
            0
            • D Driftwood
              28 Oct 2020, 16:29

              @fkaraokur - I'm not sure where you are with this, but I'll show you what works for me. From mainwidow.cpp

              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
              
                  swDB = QSqlDatabase::addDatabase("QSQLITE");
                  QDir dbDir;
                  dbDir.mkdir("db");
                  swDB.setDatabaseName("db/data.db");
              
                  if (!swDB.open())
                      ui->statusBar->showMessage(swDB.lastError().text());
                  else
                  {
                      /*
                       * THE FOLLOWING ENSURES FOREIGN KEYS WILL BE ENABLED
                       * FOR SQLITE3. THIS MUST BE THE FIRST QUERY EXECUTED
                       * WHEN THE APP STARTS.
                       */
                      QSqlQuery q(swDB);
                      q.exec("PRAGMA foreign_keys = 1");
                      qDebug() << "Foreign Keys have been enabled";
              
                      ui->statusBar->showMessage("Database connected");
                  }
                  swDB.close();
              }
              

              And in mainwindow.h put this:

              private:
                  QSqlDatabase swDB;
              

              The code works flawlessly. However, if you're concerns are based on the database being created after you delete it, that's normal. If the db isn't present when you run your code, Qt creates it. Every time.

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 28 Oct 2020, 19:40 last edited by
              #8

              @Driftwood said in database error:

              @fkaraokur - I'm not sure where you are with this, but I'll show you what works for me. From mainwidow.cpp

              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
              
                  swDB = QSqlDatabase::addDatabase("QSQLITE");
                  QDir dbDir;
                  dbDir.mkdir("db");
                  swDB.setDatabaseName("db/data.db");
              
                  if (!swDB.open())
                      ui->statusBar->showMessage(swDB.lastError().text());
                  else
                  {
                      /*
                       * THE FOLLOWING ENSURES FOREIGN KEYS WILL BE ENABLED
                       * FOR SQLITE3. THIS MUST BE THE FIRST QUERY EXECUTED
                       * WHEN THE APP STARTS.
                       */
                      QSqlQuery q(swDB);
                      q.exec("PRAGMA foreign_keys = 1");
                      qDebug() << "Foreign Keys have been enabled";
              
                      ui->statusBar->showMessage("Database connected");
                  }
                  swDB.close();
              }
              

              And in mainwindow.h put this:

              private:
                  QSqlDatabase swDB;
              

              The code works flawlessly. However, if you're concerns are based on the database being created after you delete it, that's normal. If the db isn't present when you run your code, Qt creates it. Every time.

              As the QSqlDatabase documentation warns: it's highly not recommended to keep a QSqlDatabase class member.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              D 1 Reply Last reply 29 Oct 2020, 06:34
              2
              • F fkaraokur
                28 Oct 2020, 15:21

                @jsulm said in database error:

                @fkaraokur said in database error:

                "db.db"

                This is relative path and you wrote that it is in your project folder. But your app is looking in its current working directory which is the build folder by default, so it can't find it. Deploy the database file to the build folder. Use https://doc.qt.io/qt-5/qstandardpaths.html if you deploy your application to find the database file. Also if you need to write that file you should put it in a writable location.

                When I put the db.db file in the compilation folder, it will run, right? So which is this folder? Isn't it correct if I specify the folder where the database file is located like this?

                 QString path = "D:/Qt_Project/BIST/db.db";
                    QSqlDatabase db;
                    db = QSqlDatabase::addDatabase("QSQLITE");
                    db.setDatabaseName(path);
                
                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 29 Oct 2020, 05:23 last edited by
                #9

                @fkaraokur said in database error:

                So which is this folder?

                If you're using QtCreator you can check in "Projects/General/Build directory".
                "When I put the db.db file in the compilation folder, it will run, right?" - yes, it should.

                "Isn't it correct if I specify the folder where the database file is located like this?" - of-course you can do this, but keep in mind that your app is then fixed to this location of the database file and you can only change that if you change the code and rebuild.

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

                1 Reply Last reply
                0
                • S SGaist
                  28 Oct 2020, 19:40

                  @Driftwood said in database error:

                  @fkaraokur - I'm not sure where you are with this, but I'll show you what works for me. From mainwidow.cpp

                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                  
                      swDB = QSqlDatabase::addDatabase("QSQLITE");
                      QDir dbDir;
                      dbDir.mkdir("db");
                      swDB.setDatabaseName("db/data.db");
                  
                      if (!swDB.open())
                          ui->statusBar->showMessage(swDB.lastError().text());
                      else
                      {
                          /*
                           * THE FOLLOWING ENSURES FOREIGN KEYS WILL BE ENABLED
                           * FOR SQLITE3. THIS MUST BE THE FIRST QUERY EXECUTED
                           * WHEN THE APP STARTS.
                           */
                          QSqlQuery q(swDB);
                          q.exec("PRAGMA foreign_keys = 1");
                          qDebug() << "Foreign Keys have been enabled";
                  
                          ui->statusBar->showMessage("Database connected");
                      }
                      swDB.close();
                  }
                  

                  And in mainwindow.h put this:

                  private:
                      QSqlDatabase swDB;
                  

                  The code works flawlessly. However, if you're concerns are based on the database being created after you delete it, that's normal. If the db isn't present when you run your code, Qt creates it. Every time.

                  As the QSqlDatabase documentation warns: it's highly not recommended to keep a QSqlDatabase class member.

                  D Offline
                  D Offline
                  Driftwood
                  wrote on 29 Oct 2020, 06:34 last edited by Driftwood
                  #10

                  @SGaist said in database error:

                  As the QSqlDatabase documentation warns: it's highly not recommended to keep a QSqlDatabase class member.

                  That's what I get for learning from a youtube video and not reading. Thank you for showing me that.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    fkaraokur
                    wrote on 30 Oct 2020, 07:01 last edited by
                    #11

                    There was a folder right next to where I saved the project. I thought it would be the build folder and put the db file there. and it worked. I am using my database without any problems.

                    BIST  //I was putting the db file here but it was wrong
                    build-BIST-Desktop_Qt_5_15_1_MinGW_64_bit-Debug   //fixed here
                    
                    J 1 Reply Last reply 30 Oct 2020, 07:08
                    0
                    • F fkaraokur
                      30 Oct 2020, 07:01

                      There was a folder right next to where I saved the project. I thought it would be the build folder and put the db file there. and it worked. I am using my database without any problems.

                      BIST  //I was putting the db file here but it was wrong
                      build-BIST-Desktop_Qt_5_15_1_MinGW_64_bit-Debug   //fixed here
                      
                      J Offline
                      J Offline
                      JonB
                      wrote on 30 Oct 2020, 07:08 last edited by
                      #12

                      @fkaraokur
                      I am lost as to where you are at now. Do you have some path working where it picks up an existing database as you desire, or is it still not finding any database and is creating a new one each time?

                      If you are saying it is working for you by putting the database into some directory like build-BIST-Desktop_Qt_5_15_1_MinGW_64_bit-Debug, you really should read what we have all said above. This is just not the right place to put a database file. For example, it won't work once you compile for Release, it won't exist if anybody but you runs the program, and if you did distribute your application read/writing a database in the same directory as the executable will at best be a bad idea and at worst be forbidden.

                      1 Reply Last reply
                      1

                      12/12

                      30 Oct 2020, 07:08

                      • Login

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