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. How to record a database in Android
Forum Updated to NodeBB v4.3 + New Features

How to record a database in Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
11 Posts 3 Posters 722 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on 29 Jul 2019, 08:50 last edited by
    #1

    Hi!
    How to write in Qt SQLite database in Android?
    If you just copy from the resources, this base can not be changed.
    If you create via code, you cannot open it (on Windows works).

    J J 2 Replies Last reply 29 Jul 2019, 08:52
    0
    • M Mikeeeeee
      29 Jul 2019, 08:50

      Hi!
      How to write in Qt SQLite database in Android?
      If you just copy from the resources, this base can not be changed.
      If you create via code, you cannot open it (on Windows works).

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 29 Jul 2019, 08:52 last edited by jsulm
      #2

      @mikeeeeee said in How to record a database in Android:

      If you just copy from the resources, this base can not be changed.

      It depends to which location you copy it. It needs to be writeable location.
      So: where do you copy it?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Mikeeeeee
        29 Jul 2019, 08:50

        Hi!
        How to write in Qt SQLite database in Android?
        If you just copy from the resources, this base can not be changed.
        If you create via code, you cannot open it (on Windows works).

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 29 Jul 2019, 08:56 last edited by
        #3

        @mikeeeeee
        It feels, like I answered this before ...

        ah yes:
        https://forum.qt.io/topic/105104/how-to-add-sqlite-to-qresource


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • M Offline
          M Offline
          Mikeeeeee
          wrote on 29 Jul 2019, 09:08 last edited by
          #4

          But I determine the location of the folder and add to it the name of the database. Then I can safely download the database to disk. But it's not readable.

          J 1 Reply Last reply 29 Jul 2019, 09:11
          0
          • M Mikeeeeee
            29 Jul 2019, 09:08

            But I determine the location of the folder and add to it the name of the database. Then I can safely download the database to disk. But it's not readable.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 29 Jul 2019, 09:11 last edited by
            #5

            @mikeeeeee Please show your code...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mikeeeeee
              wrote on 29 Jul 2019, 09:17 last edited by
              #6

              Tried create a new, and copy. If you copy, I can not change the database.

              void AppCore::copyDbInFolder()
              {
              requestAndroidPermissions();
              //dbAdress = getDirApp() + "/dataBase.db";
              //dbAdress.replace("/","\");

              dbAdress = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).append("/myDataBaseForBrowser.db");
              
              if( ! QFile::exists(dbAdress))
              {
                  QFile::copy(":/dataBase/dataBase.db", dbAdress); //:/icons/close.png
                  /*QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
                  db.setDatabaseName(dbAdress);
                  db.open();
                  QSqlQuery query;
                  query.exec("CREATE TABLE downloadTable ("
                             "id       INTEGER PRIMARY KEY "
                                              "NOT NULL,"
                            " nameFile TEXT,"
                             "fileUrl  TEXT"
                         ");"
                  );
                  query.exec(" CREATE TABLE historyTable ("
                                " id       INTEGER PRIMARY KEY"
                                                 " NOT NULL,"
                                 "url      TEXT,"
                                 "nameSite TEXT"
                             ");"
                  );
                  query.exec("CREATE TABLE openTabTable ("
                             "id       INTEGER NOT NULL"
                                             " PRIMARY KEY AUTOINCREMENT,"
                             "url      TEXT,"
                             "nameSite TEXT,"
                             "screen   TEXT"
                         ");"
                  );
                  db.close();*/
              }
              //QFile f1(dbAdress);
              //f1.setPermissions(QFileDevice::WriteUser);
              

              }

              J 1 Reply Last reply 29 Jul 2019, 10:33
              0
              • M Mikeeeeee
                29 Jul 2019, 09:17

                Tried create a new, and copy. If you copy, I can not change the database.

                void AppCore::copyDbInFolder()
                {
                requestAndroidPermissions();
                //dbAdress = getDirApp() + "/dataBase.db";
                //dbAdress.replace("/","\");

                dbAdress = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).append("/myDataBaseForBrowser.db");
                
                if( ! QFile::exists(dbAdress))
                {
                    QFile::copy(":/dataBase/dataBase.db", dbAdress); //:/icons/close.png
                    /*QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
                    db.setDatabaseName(dbAdress);
                    db.open();
                    QSqlQuery query;
                    query.exec("CREATE TABLE downloadTable ("
                               "id       INTEGER PRIMARY KEY "
                                                "NOT NULL,"
                              " nameFile TEXT,"
                               "fileUrl  TEXT"
                           ");"
                    );
                    query.exec(" CREATE TABLE historyTable ("
                                  " id       INTEGER PRIMARY KEY"
                                                   " NOT NULL,"
                                   "url      TEXT,"
                                   "nameSite TEXT"
                               ");"
                    );
                    query.exec("CREATE TABLE openTabTable ("
                               "id       INTEGER NOT NULL"
                                               " PRIMARY KEY AUTOINCREMENT,"
                               "url      TEXT,"
                               "nameSite TEXT,"
                               "screen   TEXT"
                           ");"
                    );
                    db.close();*/
                }
                //QFile f1(dbAdress);
                //f1.setPermissions(QFileDevice::WriteUser);
                

                }

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 29 Jul 2019, 10:33 last edited by
                #7

                @mikeeeeee In this code you commented out the part where you access your database.
                So, which part exactly does not work?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mikeeeeee
                  wrote on 29 Jul 2019, 11:34 last edited by
                  #8

                  I have a resource base. Also I tried not to copy from resources, and create a new. What I have commented? How to do it properly?

                  J 1 Reply Last reply 29 Jul 2019, 12:56
                  0
                  • M Offline
                    M Offline
                    Mikeeeeee
                    wrote on 29 Jul 2019, 12:20 last edited by
                    #9

                    void AppCore::copyDbInFolder()
                    {
                    requestAndroidPermissions();
                    //dbAdress = getDirApp() + "/dataBase.db";
                    dbAdress = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).append("/myDataBaseForBrowser.db");
                    //dbAdress = QStandardPaths::standardLocations(QStandardPaths::DataLocation) + QDir::separator() + "dataBase.db"
                    //dbAdress = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).append("/myDataBaseForBrowser.db");
                    //QFile f2(dbAdress);
                    //if (f2.remove()) {"remuve file";} else {"dont remove file";};
                    //dbAdress.replace("/","\");
                    if( ! QFile::exists(dbAdress))
                    {
                    qDebug()<<"файл с базой не существует";
                    /QFile::copy(":/dataBase/dataBase.db", dbAdress); //:/icons/close.png
                    QFile f1(dbAdress);
                    f1.setPermissions(QFileDevice::WriteUser);
                    /
                    QFile *db1 = new QFile(dbAdress);
                    if(db1->open(QFile::WriteOnly)){
                    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
                    db.setDatabaseName(dbAdress);
                    db.open();
                    QSqlQuery query;
                    query.exec("CREATE TABLE downloadTable ("
                    "id INTEGER PRIMARY KEY "
                    "NOT NULL,"
                    " nameFile TEXT,"
                    "fileUrl TEXT"
                    ");"
                    );
                    query.exec(" CREATE TABLE historyTable ("
                    " id INTEGER PRIMARY KEY"
                    " NOT NULL,"
                    "url TEXT,"
                    "nameSite TEXT"
                    ");"
                    );
                    query.exec("CREATE TABLE openTabTable ("
                    "id INTEGER NOT NULL"
                    " PRIMARY KEY AUTOINCREMENT,"
                    "url TEXT,"
                    "nameSite TEXT,"
                    "screen TEXT"
                    ");"
                    );
                    db.commit();
                    db.close();
                    }
                    }
                    else {
                    qDebug()<<"файл с базой уже существует";
                    }
                    if(QFile::exists(dbAdress)){qDebug()<<"файл с базой записался";} else {qDebug()<<"файл с базой не записался";}
                    //QFile f1(dbAdress);
                    //f1.setPermissions(QFileDevice::WriteUser);
                    }

                    1 Reply Last reply
                    0
                    • M Mikeeeeee
                      29 Jul 2019, 11:34

                      I have a resource base. Also I tried not to copy from resources, and create a new. What I have commented? How to do it properly?

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 29 Jul 2019, 12:56 last edited by jsulm
                      #10

                      @mikeeeeee said in How to record a database in Android:

                      What I have commented?

                      Do you actually read the code you post here?!
                      Here:

                      /*QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
                      

                      I'm not going to debug your code. You don't even tell us what exactly does not work (and I already ask you which part does not work, but you once more do not answer...)!
                      Did copy succeed? If so what did not work?
                      It is really hard to help you as you're asking unclear answers without needed information and do not answer questions...

                      And you last code again contains a lot commented out code. Why don't you clean it up to help others to help you?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mikeeeeee
                        wrote on 29 Jul 2019, 13:25 last edited by
                        #11

                        I posted working code and marked the topic solved. If anyone needs it, they can have my code. More in it to understand it is not necessary.

                        1 Reply Last reply
                        0

                        4/11

                        29 Jul 2019, 09:08

                        topic:navigator.unread, 7
                        • Login

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