Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Accessing to an existing SQLite DataBase from Qt
QtWS25 Last Chance

[SOLVED] Accessing to an existing SQLite DataBase from Qt

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
12 Posts 2 Posters 3.6k 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.
  • carles.sole.grauC Offline
    carles.sole.grauC Offline
    carles.sole.grau
    wrote on last edited by carles.sole.grau
    #1

    Hi everybody,
    I'm trying to read an existing SQLite database from my QML applicattion,
    right now, I have the database file imported to my project but I'm unable to acces to it, has somebody knows how to do it?

    Thank you very much

    p3c0P 1 Reply Last reply
    0
    • carles.sole.grauC carles.sole.grau

      Hi everybody,
      I'm trying to read an existing SQLite database from my QML applicattion,
      right now, I have the database file imported to my project but I'm unable to acces to it, has somebody knows how to do it?

      Thank you very much

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi @carles.sole.grau
      Are you getting any specific errors ? Did you open the database from your code?
      There's an example which explains it:
      http://doc.qt.io/qt-5/qtquick-localstorage-qmlmodule.html

      157

      1 Reply Last reply
      0
      • carles.sole.grauC Offline
        carles.sole.grauC Offline
        carles.sole.grau
        wrote on last edited by carles.sole.grau
        #3

        Hi, @p3c0
        Yes I looked this example and so far, I know how to create a SQLite database and insert and "read" data from it.

        But now, I'm interested in getting information from a external DataBase, so I would like to import it into my projecte to work with it.

        Thank you

        p3c0P 1 Reply Last reply
        0
        • carles.sole.grauC carles.sole.grau

          Hi, @p3c0
          Yes I looked this example and so far, I know how to create a SQLite database and insert and "read" data from it.

          But now, I'm interested in getting information from a external DataBase, so I would like to import it into my projecte to work with it.

          Thank you

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @carles.sole.grau

          But now, I'm interested in getting information from a external DataBase, so I would like to import it into my projecte to work with it.

          Can you elaborate ?

          157

          1 Reply Last reply
          0
          • carles.sole.grauC Offline
            carles.sole.grauC Offline
            carles.sole.grau
            wrote on last edited by
            #5

            @p3c0 ,
            Sorry for not knowing the meaning, but what exactly are you meaning with "Elaborate"?

            p3c0P 1 Reply Last reply
            0
            • carles.sole.grauC carles.sole.grau

              @p3c0 ,
              Sorry for not knowing the meaning, but what exactly are you meaning with "Elaborate"?

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @carles.sole.grau
              Thats fine. Can you give more details of what you are trying to do ?

              157

              1 Reply Last reply
              0
              • carles.sole.grauC Offline
                carles.sole.grauC Offline
                carles.sole.grau
                wrote on last edited by
                #7

                @p3c0 ,
                ups... jaja
                Of course,
                Right now i have a SQLite database that it has made using SQLite DatabaseBrowser, its extensions is .sqlite.

                For my project, I'm interested to have access to this database, to read the content so:
                *I add this file to my QML project
                *I'm trying to using it using the Javascript functions

                But I don't know how to make the program able to read (or to access) to this database.

                Thank you

                p3c0P 1 Reply Last reply
                0
                • carles.sole.grauC carles.sole.grau

                  @p3c0 ,
                  ups... jaja
                  Of course,
                  Right now i have a SQLite database that it has made using SQLite DatabaseBrowser, its extensions is .sqlite.

                  For my project, I'm interested to have access to this database, to read the content so:
                  *I add this file to my QML project
                  *I'm trying to using it using the Javascript functions

                  But I don't know how to make the program able to read (or to access) to this database.

                  Thank you

                  p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @carles.sole.grau Sorry you can't do that using purely js or QML function.s You will need C++ for that. For eg. using QSqlQueryModel.

                  157

                  1 Reply Last reply
                  0
                  • carles.sole.grauC Offline
                    carles.sole.grauC Offline
                    carles.sole.grau
                    wrote on last edited by
                    #9

                    And any example of that?

                    Thank you

                    p3c0P 1 Reply Last reply
                    0
                    • carles.sole.grauC carles.sole.grau

                      And any example of that?

                      Thank you

                      p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #10

                      @carles.sole.grau
                      Here's one:
                      https://wiki.qt.io/How_to_Use_a_QSqlQueryModel_in_QML

                      Just needs some minor modifications to make it work with Qt Quick 2.x

                      157

                      1 Reply Last reply
                      0
                      • carles.sole.grauC Offline
                        carles.sole.grauC Offline
                        carles.sole.grau
                        wrote on last edited by
                        #11

                        Thank you,
                        I will try it!!

                        1 Reply Last reply
                        1
                        • carles.sole.grauC Offline
                          carles.sole.grauC Offline
                          carles.sole.grau
                          wrote on last edited by
                          #12

                          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

                          • Login

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