PDA

View Full Version : Storing and accessing a database with QT creator



KathP
24th May 2018, 09:19
Hi

I'm a newbie and am building an app that connect to a database. It all works fine when I use the following and use my own local file

QString homeLocation = QStandardPaths::locate(QStandardPaths::HomeLocatio n, QString(), QStandardPaths::LocateDirectory);
userFilePath = homeLocation+"DBDirectory";

However, I need to store the database within the application.

During development, I still need to be able to locate and access the database outside QTCreator so need to be able to locate it using file explorer for instance.

I can't seem to be able to do it

If I use the following, the directory gets created, but I can't locate it in my file system . . .

temppath=qApp->applicationDirPath()+"/DBDirectory";

QDir dir(temppath);
if (!dir.exists()) {
dir.mkpath(".");
qDebug() << "NOT FOUND";
}
else
{
qDebug() << "FOUND";
}

equally, if I use the following the directory gets created too, but again I can't locate it . . . .

temppath = "/DBDirectory";

I apologise in advance if this is a naive question.

Lesiok
24th May 2018, 13:30
What You see with this line :
qDebug() << temppath;

ChristianEhrlicher
27th May 2018, 09:49
The correct path to store application specific data is QStandardPaths::AppDataLocation,QStandardPaths::Ap pLocalDataLocation or QStandardPaths::ConfigLocation depending on your needs.
http://doc.qt.io/qt-5/qstandardpaths.html#StandardLocation-enum

Lesiok
27th May 2018, 10:33
The correct path to store application specific data is QStandardPaths::AppDataLocation,QStandardPaths::Ap pLocalDataLocation or QStandardPaths::ConfigLocation depending on your needs.
http://doc.qt.io/qt-5/qstandardpaths.html#StandardLocation-enum
Not always. What about a portable application installed on pendrive or used by many users from the location on server ?

ChristianEhrlicher
27th May 2018, 11:10
He did not said anything from a portable application. And therefore storing it in the application directory is plain wrong (the current user may not even have write access to this directory...)

Lesiok
27th May 2018, 13:27
I wrote nothing about access rights. I just wrote that the unconditional statement The correct path to store application specific data is is not always true.