SQLite and Android
-
Hi. I have a question about development for android using QT. I have a local database. And I havent idea how can I use it in my project. What I've done:
First of all, my project directory looks like this:resources.qrc assets/localbase.db src/<files.h/cpp> qml/<files.qml>
So, I've put local database link in resources.qrc, which is look like this:
<RCC> <qresource prefix="/"> <file>qml/calendar.qml</file> <file>assets/localbase.db</file> </qresource> </RCC>
Now I can read file containing using QFile object.
QFile file(":/assets/localbase.db"); if (!file.open(QIODevice::ReadOnly)) { qInfo() << "File not found"; } else { qInfo() << file.readAll(); } file.close();
It works fine, I've got binary containing of my DB.
So, when I try to use this in database connecting, I've got that Database not found:QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(":/assets/localbase.db"); if (!db.open()) { qFatal("Database not found"); }
I've read, that this works only with full path of file. And it works for me, but it won't works for Android project. Also, I know about
:memory:
, but will be better to save database on local storage, and use it when user re-opening application.
I search about this problem, and find some solutions, which is not working for me. For example - copy file to directory.
So, can you help me to run local database both on my desktop, and also on android. If you need details of this project, files, and etc, I can publish it. If it's ecist another DB type, which working as local storage, and without priblems with locationing, let me know. Thank you. -
@AriosJentu said in SQLite and Android:
but will be better to save database on local storage, and use it when user re-opening application.
I search about this problem, and find some solutions, which is not working for me. For example - copy file to directory.yes copying the file to local storage is the way to go on Android, since even you only read from the database, the engine requires the file to be writeable.
to what folder did you copy it?You need to copy it from qrc to a writeable location (uisng QStandardPaths for example)
[edit: fixed class name SGaist]