[SOLVED]What would be the best way to have database support in my application.
-
Hi,
You need this line to ensure that your database file is in a known writable location. Opening it in the same folder as your executable might work when developing but as soon as you'll install your application it will likely be in a read-only folder.
Also, if your application is meant to be used by several users you'll have a security and privacy problem. One user will see the data of another or overwrite it which will be bad.
With SQLite, if the file given doesn't exist, it will be created to store the database.
-
You don't need to create your database programmatically. You could create an empty database with all your tables and relationships and store it as a resource in your app. Then you would copy this resource to a writable location if it didn't exist yet. That's what I suggested. It would be like a template database.
-
The location created from this command
db_path = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)).filePath("database.db");
does not seem to actually exist in my system, moreover the
copy
command fails.Is it normal that
db_path
returns a location that does not exist? -
What value are you getting ?
-
Looks good
What is your problem with it ?
-
Maybe you think the folder doesn't exist because the "AppData" folder is hidden by default.
-
Is ":assets" Android specific? Also should it be "assets:/datatbase...."
-
QStandardPaths returns a writable path for you. It doesn't guarantee you that it exists. If if doesn't, create it using QDir.mkpath()
http://doc.qt.io/qt-5/qdir.html#mkpath
As you know it's a writable location, it won't be a problem.
=========
@rturrentine is right also. ":assets" is for android. For Qt resources, you should use ":/database/database.db".
-
BTW, here is a link to a related post: