Show data from Database in Android app
-
wrote on 18 Oct 2014, 07:12 last edited by
Hello,
I'm new with Qt Mobile, I have two question about working with database:
How can I show data from database in android app?
How can I add/modify data and save to database?
Thanks in advance!
-
wrote on 18 Oct 2014, 09:17 last edited by
Hello,
There are a lot of example in Qt creator if you search with key "sql"
Your work cross over the platform. -
Hi,
To add to mr_wallyit's answer you can use sqlite database for that purpose. Qt has inbuilt support for SQLite db. It works on android too.
bq. How can I show data from database in android app?
You can build an app and put the database file in the Qt Resource System. Once deployed on android just copy that file from Resources to local path and then the rest would be normal database transactions.
bq. How can I add/modify data and save to database?
Use the "QSqlDatabase":http://qt-project.org/doc/qt-5/QSqlDatabase.html and it's friends to operate on the database.
-
wrote on 26 Oct 2014, 01:33 last edited by
Thanks for your answers and sorry for my late response...
I'm have been searching in the web, but I haven't enough documentation about it...
I not found how include my database in Qt Resource System and not found how to show in screen data from database.
Have you found some documentation or example?
-
You just add the database file like you add the other files into the qrc file. From the Qt Creator, create a Qt Resource file (.qrc), right click on it then add your existing database file. You can add a prefix if you want to keep files categorized.
Later from the main.cpp the first thing you would want to do is to copy that file from qrc to local path. Something like this:
@
QString dbfilepath = QDir::currentPath()+"/sample.db";
if(!QFile::exists(dbfilepath))
{
QFile::copy(":/db/sample.db", QDir::currentPath()+"/sample.db");
QFile::setPermissions(QDir::currentPath()+"/sample.db",
QFile::ReadOwner|QFile::WriteOwner);
}
@here /db in /db/sample.db is the prefix.
Then as usual you need to open it for the transactions,
@
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("sample.db");
bool ret = db.open();
if (!ret)
{
qDebug() << "Error opening database";
return false;
}
return true;
@More links
"http://qt-project.org/doc/qt-5/sql-programming.html":http://qt-project.org/doc/qt-5/sql-programming.html
"http://qt-project.org/doc/qt-5/examples-sql.html":http://qt-project.org/doc/qt-5/examples-sql.html -
wrote on 26 Oct 2014, 20:26 last edited by
Hello,
Why do you want copy database?
Library of Sqlite creates automatically file if it not exitsts when you create tables.QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("sample.db");
bool ret = db.open();
if (!ret)
{
qDebug() << "Error opening database";
return false;
}
QSqlQuery query(db);
query.exec("create table employee(id int primary key, name varchar(20), city int, country int)");
return true; -
Yes it does. I thought OP wanted his existing database to be deployed.
-
wrote on 28 Oct 2014, 06:42 last edited by
Thanks for your help expecially to "p3c0":http://qt-project.org/mebmer/139583...
In the first link that you suggested I found that with QSqlQuery I can excecute sql queries and with:
- "Model Views":http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-modelview.html
- and create "Models Using C++ with Qt Quick Views":http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html
I can show data from database... I not tested save data to database but I can is possible...
And also copy database to deploy project work perfectly...
If somebody know, How can I do that some code only execute during installation? For create database, tables and sample data the first time that the application is installed.
I appreciate your help a lot of.
-
Have you made sure that the file exists with the specified prefix in the resource file ?
bq. How can I do that some code only execute during installation?
Since you are working on the android app you can use the assets.
Check "this":http://stackoverflow.com/questions/20573838/qt-5-2-including-external-file-into-an-android-package too.
Or you can create the database on first run of the application on the target machine.
An example "here":http://qt-project.org/doc/qt-5/qtsql-cachedtable-example.html
If you check the createConnection() you can see they create the database first.Edited
-
wrote on 22 Dec 2014, 16:01 last edited by
Hello,
If you copy database from resource to folder in the internal storage, you need to change permission, because when you copy the file (database) is only to read.
-
wrote on 22 Dec 2014, 16:01 last edited by
Hello,
If you copy database from resource to folder in the internal storage, you need to change permission, because when you copy the file (database) is only to read.