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

Relative Path Needed for db in SQLite

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.6k 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.
  • C Offline
    C Offline
    CoreyWhite
    wrote on 7 Mar 2015, 20:15 last edited by tekojo 3 Jul 2015, 20:20
    #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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Mar 2015, 20:35 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
      • C Offline
        C Offline
        CoreyWhite
        wrote on 8 Mar 2015, 16:33 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 8 Mar 2015, 20:52 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

          1/4

          7 Mar 2015, 20:15

          • Login

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