Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SQLite and Android
Forum Updated to NodeBB v4.3 + New Features

SQLite and Android

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 380 Views 1 Watching
  • 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.
  • A Offline
    A Offline
    AriosJentu
    wrote on 3 Jun 2019, 18:19 last edited by AriosJentu 6 Mar 2019, 18:33
    #1

    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.

    R 1 Reply Last reply 3 Jun 2019, 19:37
    0
    • A AriosJentu
      3 Jun 2019, 18:19

      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.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 3 Jun 2019, 19:37 last edited by SGaist 6 Mar 2019, 19:54
      #2

      @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]

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2

      1/2

      3 Jun 2019, 18:19

      • Login

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