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. SQLite DB at Runtime

SQLite DB at Runtime

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 1.4k 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 Christian Ehrlicher
    25 Aug 2019, 18:01

    So what's the output of your new code? Does it work now?

    S Offline
    S Offline
    Silenzio76
    wrote on 25 Aug 2019, 18:10 last edited by
    #5

    @christian-ehrlicher no, I need a persistent database file.
    ":memory:" means in-memory cached db.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Aug 2019, 18:23 last edited by SGaist
      #6

      Hi,

      That is correct and you had it wrong in your original code.

      @silenzio76 said in SQLite DB at Runtime:

      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

      Change ":memory:" to the path you want to use and it should work correctly now.

      One additional thing, you should use QStandardPaths to get a suitable folder to store your database. On most OS, installing an application is done in a read-only part of your system, so putting your database in the same folder as your application will fail.

      [edit: fixed unclear phrase about path SGaist]

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 25 Aug 2019, 18:37
      1
      • S SGaist
        25 Aug 2019, 18:23

        Hi,

        That is correct and you had it wrong in your original code.

        @silenzio76 said in SQLite DB at Runtime:

        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

        Change ":memory:" to the path you want to use and it should work correctly now.

        One additional thing, you should use QStandardPaths to get a suitable folder to store your database. On most OS, installing an application is done in a read-only part of your system, so putting your database in the same folder as your application will fail.

        [edit: fixed unclear phrase about path SGaist]

        S Offline
        S Offline
        Silenzio76
        wrote on 25 Aug 2019, 18:37 last edited by
        #7

        @sgaist Obvously it works then how can i create the db file at runtime?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 25 Aug 2019, 18:50 last edited by
          #8

          Meaning allow the user to select the folder where to store the database ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply 25 Aug 2019, 18:52
          0
          • S SGaist
            25 Aug 2019, 18:50

            Meaning allow the user to select the folder where to store the database ?

            S Offline
            S Offline
            Silenzio76
            wrote on 25 Aug 2019, 18:52 last edited by
            #9

            @sgaist that will be in a second phase, for now I need create the db file( db.sqlite ?)at runtime

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 25 Aug 2019, 18:56 last edited by
              #10

              If the file doesn't exist, it will be created. That's how SQLite works (it's nothing related to the plugin).

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply 25 Aug 2019, 19:04
              0
              • S SGaist
                25 Aug 2019, 18:56

                If the file doesn't exist, it will be created. That's how SQLite works (it's nothing related to the plugin).

                S Offline
                S Offline
                Silenzio76
                wrote on 25 Aug 2019, 19:04 last edited by
                #11

                @sgaist ok, if will be created my question is where is put the file qith the ":memory: condition after the app is terminated?
                Because the next time I will need to connect to it and retrieve the saved datas.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 25 Aug 2019, 19:05 last edited by
                  #12

                  There's no file created when using the special keyword ":memory:".

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  S 1 Reply Last reply 25 Aug 2019, 19:17
                  0
                  • S SGaist
                    25 Aug 2019, 19:05

                    There's no file created when using the special keyword ":memory:".

                    S Offline
                    S Offline
                    Silenzio76
                    wrote on 25 Aug 2019, 19:17 last edited by
                    #13

                    @sgaist the problem is that, I need to create the file at the first start of the app.

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 25 Aug 2019, 19:21 last edited by
                      #14

                      @silenzio76 said in SQLite DB at Runtime:

                      the problem is that, I need to create the file at the first start of the app.

                      So where is your problem - simply pass the filename to setDatabaseName() instead ":memory:"...

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      S 1 Reply Last reply 25 Aug 2019, 19:30
                      0
                      • C Christian Ehrlicher
                        25 Aug 2019, 19:21

                        @silenzio76 said in SQLite DB at Runtime:

                        the problem is that, I need to create the file at the first start of the app.

                        So where is your problem - simply pass the filename to setDatabaseName() instead ":memory:"...

                        S Offline
                        S Offline
                        Silenzio76
                        wrote on 25 Aug 2019, 19:30 last edited by
                        #15

                        @christian-ehrlicher done but don't work, it's in the first post:

                        db.setDatabaseName(dbPath);
                        
                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 25 Aug 2019, 21:26 last edited by
                          #16

                          In your first post, your db object is invalid. addDatabase is a static method that returns a QSqlDatabase object that is the one that you should use to setup the database connection. Hence my comment on your new version using :memory:., you are using QSqlDatabase correctly on that one. So as already said, you can now use a path to a database file. If it's still not working, then check the errors and also check that you have the write permission on the folder you want to store your database in.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1
                          • O Offline
                            O Offline
                            Oshio
                            wrote on 26 Aug 2019, 03:00 last edited by Oshio
                            #17

                            @Silenzio76
                            I don't have the time to read your code, but take a look at my SQLiteSave library: https://github.com/Oshio09/Oshio_Qt_Static_Libs

                            Its not documented but you will find the interface very intuitive. Try to maintain a copy of the repository, because I just created after I saw your question. I may delete or rename it in the future.

                            There is a QT Test project that I wrote, read and use as an example on how to use the library.
                            I don't know how much you understand about Qt so I wrote some simple steps on how to run the tests at the README file.

                            I wrote this lib last week so I didn't had time to do an extensive test, try to run and let me know of any errors. As @SGaist pointed out QStandardPaths is good practice, SQLiteSave makes use of it.

                            S 1 Reply Last reply 26 Aug 2019, 13:09
                            0
                            • O Oshio
                              26 Aug 2019, 03:00

                              @Silenzio76
                              I don't have the time to read your code, but take a look at my SQLiteSave library: https://github.com/Oshio09/Oshio_Qt_Static_Libs

                              Its not documented but you will find the interface very intuitive. Try to maintain a copy of the repository, because I just created after I saw your question. I may delete or rename it in the future.

                              There is a QT Test project that I wrote, read and use as an example on how to use the library.
                              I don't know how much you understand about Qt so I wrote some simple steps on how to run the tests at the README file.

                              I wrote this lib last week so I didn't had time to do an extensive test, try to run and let me know of any errors. As @SGaist pointed out QStandardPaths is good practice, SQLiteSave makes use of it.

                              S Offline
                              S Offline
                              Silenzio76
                              wrote on 26 Aug 2019, 13:09 last edited by
                              #18

                              @oshio Thank You, you'ra are very usefull!!!

                              1 Reply Last reply
                              0

                              14/18

                              25 Aug 2019, 19:21

                              • Login

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