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
Forum Updated to NodeBB v4.3 + New Features

SQLite DB at Runtime

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 1.6k Views 2 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    I would gues you simply don't use the result of QSqlDatabase::addDatabase() correctly as explained in the documentation: https://doc.qt.io/qt-5/qsqldatabase.html#details

    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
    0
    • Christian EhrlicherC Christian Ehrlicher

      I would gues you simply don't use the result of QSqlDatabase::addDatabase() correctly as explained in the documentation: https://doc.qt.io/qt-5/qsqldatabase.html#details

      S Offline
      S Offline
      Silenzio76
      wrote on last edited by
      #3

      @christian-ehrlicher I followed the Books example, with the addition of the path:

       QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
          db.setDatabaseName(":memory:");
      
          if (!db.open())
              return db.lastError();
      
      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

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

        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
        0
        • Christian EhrlicherC Christian Ehrlicher

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

          S Offline
          S Offline
          Silenzio76
          wrote on last edited by
          #5

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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
            1
            • SGaistS SGaist

              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 last edited by
              #7

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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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
                0
                • SGaistS SGaist

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

                  S Offline
                  S Offline
                  Silenzio76
                  wrote on 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
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 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
                    0
                    • SGaistS SGaist

                      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 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
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 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
                        0
                        • SGaistS SGaist

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

                          S Offline
                          S Offline
                          Silenzio76
                          wrote on 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
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 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
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              @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 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
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 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 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
                                  0
                                  • O Oshio

                                    @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 last edited by
                                    #18

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

                                    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