Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Show data from Database in Android app
Forum Update on Monday, May 27th 2025

Show data from Database in Android app

Scheduled Pinned Locked Moved Mobile and Embedded
11 Posts 3 Posters 4.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    omixam
    wrote on 18 Oct 2014, 07:12 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mr_wallyit
      wrote on 18 Oct 2014, 09:17 last edited by
      #2

      Hello,
      There are a lot of example in Qt creator if you search with key "sql"
      Your work cross over the platform.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        p3c0
        Moderators
        wrote on 18 Oct 2014, 10:46 last edited by
        #3

        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.

        157

        1 Reply Last reply
        0
        • O Offline
          O Offline
          omixam
          wrote on 26 Oct 2014, 01:33 last edited by
          #4

          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?

          1 Reply Last reply
          0
          • P Offline
            P Offline
            p3c0
            Moderators
            wrote on 26 Oct 2014, 05:51 last edited by
            #5

            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

            157

            1 Reply Last reply
            1
            • M Offline
              M Offline
              mr_wallyit
              wrote on 26 Oct 2014, 20:26 last edited by
              #6

              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;

              1 Reply Last reply
              0
              • P Offline
                P Offline
                p3c0
                Moderators
                wrote on 27 Oct 2014, 04:43 last edited by
                #7

                Yes it does. I thought OP wanted his existing database to be deployed.

                157

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  omixam
                  wrote on 28 Oct 2014, 06:42 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    p3c0
                    Moderators
                    wrote on 28 Oct 2014, 07:00 last edited by
                    #9

                    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

                    157

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mr_wallyit
                      wrote on 22 Dec 2014, 16:01 last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mr_wallyit
                        wrote on 22 Dec 2014, 16:01 last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved