Qt Mobile Application for Android
-
I am developing an offline android Application using Qt.Basically my application will access user's information (say for example user_name ,age,gender) from Sqlite database which is an offline database(which i want to embedd into my APP) .now my question is how i can embedd this sqlite db file into my application.after doing some research online i got two methods by which i can provide database file along with my application.
now i have a dilemma in choosing the right one
1.should i use in-memory database,(if yes please give me an example on how can i create an in-memory database and dump that to a binary qrc file)
2. or should i put it into regular files and distribute them along with my application( for this procedure i learnt that i need to include below lines of code in my .pro file . is it true ? if not tell me a proper way through which i can add database file into my application.addFiles.sources =applicationdb2
addFiles.path = .
DEPLOYMENT += addFiles ) -
Hi and welcome to devnet,
In memory database is not what you since as soon as your application stops, the database is destroyed.
The most classical way would be to create the database in the correct location using QStandardPaths using
DataLocation
. If the database is empty by default then you can simply create it if doesn't exist at the start of your application. -
@SGaist actually in my case I need to attach a database file which has defualt values loaded into it along with my application . I think this purpose can serve better with in-memory database.from the below link I got the idea of in-memory but I don't know how I can create an in-memory db and dump it to a qrc file .
https://stackoverflow.com/questions/4254250/embedded-database-in-qtOr please tell me any other ways through which I can provide database file along with my application.
FYI my application is an android app. -
The most simple is to just create the database in file as normal and then copy that file into your project folder.
-
@divaindie simply add the file to a .qrc
I'm providing per ex a JSON settings file this way:<RCC> <qresource prefix="/"> <file>data-assets/cacheSettingsData.json</file> </qresource> </RCC>
then in my app I'm checking if the file already exists in my appdata - if not I copy it
-
@ekkescorner I don't know about JSON file .but adding a database file in qrc doesn't work .you can see this link https://stackoverflow.com/questions/4254250/embedded-database-in-qt
-
@divaindie basically we can add only binary files into qrc. during compilation of Qt application ,qmake will create a file called qrc_application.cpp file from .qrc file .
.this qrc_application.cpp will have pointers to binary dump of resources which are added into .qrc file before building .This link will explain more about qrc system in Qt http://doc.qt.io/qt-5/resources.html
suppose if we include a database file(which is not a binary file) into .qrc ,it will not work because "The database name is just the path to the file. Qt does not intercept the file name but hands it over directly to the SQLite functions (by calling toUtf8() on the “path”), which in turn try to open that file – and will eventually fail, of course."
You can find this in below qt forum thread
https://forum.qt.io/topic/3949/sqlite-database-in-project/3This is the reason why I found in-memory database is the solution to my problem .If you see this link you will get why I had choosen in-memory database
https://stackoverflow.com/questions/4254250/embedded-database-in-qt
But I don't know how to do it .If you have the solution /or any alternative solution let me know -
@divaindie have you tried it this way ? https://stackoverflow.com/a/40713888/135559
or this: https://forum.qt.io/topic/94088/deploy-sqlite-database-with-apk/12
-
@ekkescorner i tried the link https://forum.qt.io/topic/94088/deploy-sqlite-database-with-apk/12
but i got below error message while copying db file to a writable location ( i.e QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppDataLocation); )
W QtMainThread: type=1400 audit(0.0:119): avc: denied { link } for name="qt_temp.xajbeT" dev="dm-0" ino=124288 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=file permissive=0
edit1 : it works if i use "QStandardPaths::GenericDataLocation" instead of "QStandardPaths::StandardLocation::AppDataLocation".
now my question is which location i should prefer using to copy my database file from assets:/ folder ? .(FYI both read/write opearion allowed on database file) -
@divaindie what's your Qt version ?
there were issues with temp files causing errors while copying on Android
5.10.1 and 5.11.1 should be ok -
@ekkescorner iam using 5.10.1 !!! .which version of Qt works fine for developing Android applications?
-
@divaindie 5.10.1 should be fine.
can you create, save and copy a normal text file at your AppDataLocation ? -
@ekkescorner Iam able to create files.but i cant read/write/copy files to AppDataLocation
-
@divaindie curios.
I'm using this to get the appdata locationmDataRoot = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).value(0);
then I'm checking if the directory exists, but this should exists on Android, but must be created on iOS
to be safe I'm always checkingQDir myDir; bool exists; exists = myDir.exists(mDataRoot); if (!exists) { bool ok = myDir.mkpath(mDataRoot); if(!ok) { qWarning() << "Couldn't create mDataRoot " << mDataRoot; return false; } qDebug() << "created directory mDataRoot " << mDataRoot; }
then as next I'm creating a /data/ directory and place all my other files ond folders inside this
never had problems with read write delete copy -
@ekkescorner this worked for me.thanks!!.
for linux & windows if i want to add my database file what changes i need to make in .pro file ?.
for android i made below changes.
android
{
my_files.path = /assets
my_files.files = $$PWD/android/*
INSTALLS += my_files
} -
@divaindie good to hear that it's working for you
-
@divaindie said in Qt Mobile Application for Android:
for linux & windows if i want to add my database file what changes i need to make in .pro file ?
Maybe it'd be good you mark this post as solved since you originally asked for Android and it's working for you now, and open a new post for the other platforms so not to hijack the post. Thanks.