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

    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
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @fkaraokur said in database error:

    it does not give an error and it does not connect to the database

    But it always works connected.

    Personally I don't understand. You talk about it both not connecting and connecting.... All I will say is: since your path is just a filename with no path, it will only look in the current directory at the moment this code is executed. There is no reason that should be "the project folder". Why don't you make it a proper full path to the desired file and then see how you go?

    Ahh, that rings a bell. I believe SQLite works by: if it finds the file you give it to open it uses that, but if the file/path does not exist SQLite creates a new database/file at that path. Is this perhaps what you are seeing/confusing you?

    1 Reply Last reply
    3
    • F fkaraokur

      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
      
      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on 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
      3
      • B Offline
        B Offline
        Bonnie
        wrote on 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
        • jsulmJ jsulm

          @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 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);
          
          JonBJ jsulmJ 2 Replies Last reply
          0
          • F fkaraokur

            @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);
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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
            • DriftwoodD Offline
              DriftwoodD Offline
              Driftwood
              wrote on 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.

              SGaistS 1 Reply Last reply
              0
              • DriftwoodD Driftwood

                @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.

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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

                DriftwoodD 1 Reply Last reply
                2
                • F fkaraokur

                  @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);
                  
                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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
                  • SGaistS SGaist

                    @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.

                    DriftwoodD Offline
                    DriftwoodD Offline
                    Driftwood
                    wrote on 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 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
                      
                      JonBJ 1 Reply Last reply
                      0
                      • F fkaraokur

                        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
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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

                        • Login

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