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

[SOLVED] Accessing to an existing SQLite DataBase from Qt

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
12 Posts 2 Posters 3.7k 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.
  • C Offline
    C Offline
    carles.sole.grau
    wrote on 29 Apr 2016, 09:11 last edited by carles.sole.grau 5 Jan 2016, 11:54
    #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

    P 1 Reply Last reply 29 Apr 2016, 09:20
    0
    • C carles.sole.grau
      29 Apr 2016, 09:11

      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

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 29 Apr 2016, 09:20 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
      • C Offline
        C Offline
        carles.sole.grau
        wrote on 29 Apr 2016, 09:26 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

        P 1 Reply Last reply 29 Apr 2016, 09:28
        0
        • C carles.sole.grau
          29 Apr 2016, 09:26

          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

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 29 Apr 2016, 09:28 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
          • C Offline
            C Offline
            carles.sole.grau
            wrote on 29 Apr 2016, 09:33 last edited by
            #5

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

            P 1 Reply Last reply 29 Apr 2016, 09:35
            0
            • C carles.sole.grau
              29 Apr 2016, 09:33

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

              P Offline
              P Offline
              p3c0
              Moderators
              wrote on 29 Apr 2016, 09:35 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
              • C Offline
                C Offline
                carles.sole.grau
                wrote on 29 Apr 2016, 09:43 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

                P 1 Reply Last reply 29 Apr 2016, 09:51
                0
                • C carles.sole.grau
                  29 Apr 2016, 09:43

                  @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

                  P Offline
                  P Offline
                  p3c0
                  Moderators
                  wrote on 29 Apr 2016, 09:51 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
                  • C Offline
                    C Offline
                    carles.sole.grau
                    wrote on 29 Apr 2016, 10:40 last edited by
                    #9

                    And any example of that?

                    Thank you

                    P 1 Reply Last reply 29 Apr 2016, 10:47
                    0
                    • C carles.sole.grau
                      29 Apr 2016, 10:40

                      And any example of that?

                      Thank you

                      P Offline
                      P Offline
                      p3c0
                      Moderators
                      wrote on 29 Apr 2016, 10:47 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
                      • C Offline
                        C Offline
                        carles.sole.grau
                        wrote on 29 Apr 2016, 10:48 last edited by
                        #11

                        Thank you,
                        I will try it!!

                        1 Reply Last reply
                        1
                        • C Offline
                          C Offline
                          carles.sole.grau
                          wrote on 1 May 2016, 11:53 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

                          4/12

                          29 Apr 2016, 09:28

                          topic:navigator.unread, 8
                          • Login

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