Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Relative Path Needed for db in SQLite

    General and Desktop
    2
    4
    3817
    Loading More Posts
    • 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.
    • CoreyWhite
      CoreyWhite last edited by tekojo

      The following code always crashes when it tries to open the database. If I give it a full absolute path it runs fine. I can't expect my users to all have the same path. So what gives? Thanks.

      #include <QApplication>
      #include <QMessageBox>
      #include <QtSql>
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      QStringList db;

      void load_db0(){

      QSqlDatabase db_new = QSqlDatabase::addDatabase("QSQLITE");
      QString dbPath = QCoreApplication::applicationDirPath() + "/mydb.db";
      db_new.setDatabaseName(dbPath);
      db_new.open();
      
      QSqlQuery query;
      if(query.exec("SELECT * FROM phictionphreak")){
          while(query.next()){
              db << query.value(0).toString();
          }
      
      }
      

      db_new.close();
      }

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Don't put that database in the application folder. Take for example Windows, normal users can't write to the application folder. What you should do is use QStandardPaths with QStandardPaths::AppDataLocation to retrieve a valid path to store that database. Doing so you'll ensure that your users will be able do use it no matter what the OS is

        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 Reply Quote 0
        • CoreyWhite
          CoreyWhite last edited by

          Thanks SGaist, but that isn't quite the answer I am looking for. Right now I am playing with these 3 functions to try to get the directory that my executable is in, wherever it is in. I want the user to store the database wherever he has my executable.

          Could you tell me which of these 3 options is the best, or if you know another one?

          1: QString path = qApp->applicationDirPath();
          2: QDir::currentPath();
          3: QDir dir;
          dir.absolutePath();

          Thanks a bunch!

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Number one is the correct solution but it will probably fail on OS X, that's why I'm suggesting the use of QStandardPaths

            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 Reply Quote 0
            • First post
              Last post