My Qt C++ project involves dynamically creating and working with SQLite databases.
I'm encapsulating everything to do with the databases inside a class called Database. Here is my method to either create a database file if it doesn't exist, or open it if it does:
void Database::connectDB(QString databaseName)
{
QString fPath = QCoreApplication::applicationDirPath();
fileName = fPath + "/DBs/" + databaseName + ".db";
db = QSqlDatabase::database(connectionName);
db.setDatabaseName(fileName);
}
connectionName
and fileName
are QStrings created elsewhere and are valid. To put it simply, this code works. But only when run from Qt Creator. I've verified that the absolute path I feed to setDatabaseName
overrides the working directory as shown in the Qt Creator project settings. I do have to manually create the folder DBs. Perhaps that's not the best way to go about it, but I don't think it impacts this situation. After all, I have to create it before running from Qt Creator too.
I deploy on Windows 10 using the steps from the following video: https://www.youtube.com/watch?v=8qozxqSZQEg
I think the --quick
argument is for Qt Quick, which I'm not using. I've tried with and without.
After deployment, the application looks like it's working, but the databases it needs are never created, so it eventually crashes. It surprises me that it doesn't immediately crash. I could be mistaken, but it almost seems that it's creating the first few databases in memory. Some information that should only be available by reading data previously entered into a database does show up. But not if I close and restart.
I've read this question and the linked questions, but they don't really have much to do with the exact nature of this issue: Path to the project current dir in qt
I can't wrap my head around why this isn't working. I've created DBs folders in every parent folder as well, just to be sure. Nothing. The files are simply not created. It works in the IDE, not once deployed. What is Qt Creator providing that isn't available to the application after deployment?
The compiler I'm using is msvc2015_64, if that matters. If there are any other details I'm leaving out that could be helpful, please mention them. I'll edit them in.