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. Relative Path Needed for db in SQLite
Qt 6.11 is out! See what's new in the release blog

Relative Path Needed for db in SQLite

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 5.3k Views 2 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.
  • CoreyWhiteC Offline
    CoreyWhiteC Offline
    CoreyWhite
    wrote on last edited by tekojo
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • CoreyWhiteC Offline
        CoreyWhiteC Offline
        CoreyWhite
        wrote on last edited by
        #3

        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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0

          • Login

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