Relative Path Needed for db in SQLite
-
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();
} -
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
-
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!
-
Number one is the correct solution but it will probably fail on OS X, that's why I'm suggesting the use of QStandardPaths