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. Import an External SQLite database to my project
Forum Updated to NodeBB v4.3 + New Features

Import an External SQLite database to my project

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.5k 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.
  • C Offline
    C Offline
    carles.sole.grau
    wrote on 29 Apr 2016, 10:53 last edited by A Former User 5 Sept 2016, 11:12
    #1

    Hi everybody,
    I'm interested in importing an external SQLite database to my project.
    When using the QT Quick Local Storage

    LocalStorage.openDatabaseSync("QQmlExampleDB", "1.0", "The Example QML SQL!", 1000000);
    

    The problem is that, the program generates a NEW database, and if it's possible I'm interested in open an existing database.

    Any idea?

    Thank you very much!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      aliks-os
      wrote on 29 Apr 2016, 11:21 last edited by
      #2

      The ideas are two:

      1. copies all tables from one DB to another
      2. change name of DB file and replace one DB on another
      1 Reply Last reply
      0
      • C Offline
        C Offline
        carles.sole.grau
        wrote on 29 Apr 2016, 13:11 last edited by
        #3

        HI @aliks-os ,
        thanks for your answer,
        and how I could to for example the first idea, copy all tables from one DB to another?

        My problem is that I don't know how to acces to the first database on QML.

        Thank you

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aliks-os
          wrote on 30 Apr 2016, 10:33 last edited by
          #4

          I think you need change your code in QML/Qt to open at once two DB.
          What the problem to have access to the first DB?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 30 Apr 2016, 10:52 last edited by
            #5

            @carles.sole.grau said:

            Hi I dont have QML installed so cant test but I was wondering
            Docs at
            http://doc.qt.io/qt-5/qtquick-localstorage-qmlmodule.html
            says
            "They are stored in the Databases subdirectory of QQmlEngine::offlineStoragePath()"

            So maybe its possible to run your code so it creates a new database
            then output the path from offlineStoragePath()
            Then go there with Explorer
            Close your app.
            and copy over the new DB QQmlExampleDB with the
            full db you have also renamed to QQmlExampleDB
            or simply place the new db there and see if it then can open it?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              carles.sole.grau
              wrote on 1 May 2016, 11:39 last edited by
              #6

              Finally, I could solve my problem, all the things I have had to do are:

              In main.cpp I add this code:

              QDir dir("./Databases");
              if (!dir.exists()) {
                  dir.mkpath(".");
              }
              QString new_name = QString(QCryptographicHash::hash(("nameofthecopiedDB"),QCryptographicHash::Md5).toHex());
              
              QFile file(":/SQLite/nameofsourceDB.sqlite");
              file.copy("./Databases/" + new_name + ".sqlite");
              file.close();
              

              Since I have the DB I would to use in my project in SQLite folder from my resources (:, indicates resources).
              The QCryptographicHash::hash function is necessary because in the:

              openDatabaseSync(string name, string version, string description, int estimated_size, jsobject callback(db))
              

              the name is passed with md5 codification: "the path to a database is QQmlEngine::offlineStoragePafh + md5(name) + ".sqlite". Where name is 1st parameter of openDatabaseSync"

              And then, in QML file, the openDatabaseSync() function:

              db = Sql.LocalStorage.openDatabaseSync('nameofthecopiedDB',"1.0","description",1000000,"QSQLITE")
              

              Thank you!

              1 Reply Last reply
              1

              1/6

              29 Apr 2016, 10:53

              • Login

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