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 749 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 Mikeeeeee

    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).

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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

      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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on 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 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.

        jsulmJ 1 Reply Last reply
        0
        • M Mikeeeeee

          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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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 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);
            

            }

            jsulmJ 1 Reply Last reply
            0
            • M Mikeeeeee

              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);
              

              }

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 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 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?

                jsulmJ 1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mikeeeeee
                  wrote on 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

                    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?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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 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

                      • Login

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