Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

database error



  • 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
    


  • @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?


  • Lifetime Qt Champion

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



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



  • @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);
    


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



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


  • Lifetime Qt Champion

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


  • Lifetime Qt Champion

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



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



  • 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
    


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


Log in to reply