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 is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path
Forum Updated to NodeBB v4.3 + New Features

Sqlite DB is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 855 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.
  • S Offline
    S Offline
    SocketSackett
    wrote on 2 Dec 2021, 01:46 last edited by
    #1

    Sqlite DB is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path.

    P 1 Reply Last reply 2 Dec 2021, 01:50
    0
    • S SocketSackett
      2 Dec 2021, 01:46

      Sqlite DB is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path.

      P Offline
      P Offline
      Pl45m4
      wrote on 2 Dec 2021, 01:50 last edited by Pl45m4 12 Feb 2021, 02:02
      #2

      @SocketSackett

      Please show what you did exactly.
      If you specify a *.db file, it should be there, as long as you don't face any errors while opening the DB connection.

      For the QSQLITE driver, if the database name specified does not exist, then it will create the file for you unless the QSQLITE_OPEN_READONLY option is set.

      Additionally, name can be set to ":memory:" which will create a temporary database which is only available for the lifetime of the application.

      (from: https://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      S 1 Reply Last reply 2 Dec 2021, 17:54
      0
      • S Offline
        S Offline
        SocketSackett
        wrote on 2 Dec 2021, 02:12 last edited by
        #3

        It didn't let me finish typing. The once every 10 mins is a bit controlling! This is a RW database.

        The funny thing is I had this was working before I added in DB creation by code instead of the tool, but something changed somewhere, perhaps an environmental change. But when I use the Sqlite tools, I see it created the DB file, dataman.db, it says the DB is a "transient in-memory database". The file is created on disk, but somewhere in the bowels of libraries it is not creating it properly. It is quite befuddling.

        My source is pretty simple:

        bool MdDatabaseManager::connectDb()
        {
        bool rc = false;
        bool opened = false;
        bool pre_exists = getPaths().existsDb();
        m_pathDb = getPaths().makeDbPath();

            if (!pre_exists || (pre_exists && !m_db.isValid()))
            {
                m_db = QSqlDatabase::addDatabase("QSQLITE"); // where it holds connection            
                m_db.setDatabaseName(m_pathDb);            
            }
            bool post_exists = getPaths().existsDb(); // post check if it created it
                    
            if (!(opened = m_db.isOpen()))        
                opened = m_db.open();            
        
            if (!opened)
            {
                qDebug() << "Error: connection with database failed";
            }
            else
            {
                rc = true;
                qDebug() << "Database: connection ok";
                if (!pre_exists && post_exists)
                {
                    m_created = true;                
                }
            }
            return rc;
        
        J P 2 Replies Last reply 2 Dec 2021, 07:45
        0
        • S SocketSackett
          2 Dec 2021, 02:12

          It didn't let me finish typing. The once every 10 mins is a bit controlling! This is a RW database.

          The funny thing is I had this was working before I added in DB creation by code instead of the tool, but something changed somewhere, perhaps an environmental change. But when I use the Sqlite tools, I see it created the DB file, dataman.db, it says the DB is a "transient in-memory database". The file is created on disk, but somewhere in the bowels of libraries it is not creating it properly. It is quite befuddling.

          My source is pretty simple:

          bool MdDatabaseManager::connectDb()
          {
          bool rc = false;
          bool opened = false;
          bool pre_exists = getPaths().existsDb();
          m_pathDb = getPaths().makeDbPath();

              if (!pre_exists || (pre_exists && !m_db.isValid()))
              {
                  m_db = QSqlDatabase::addDatabase("QSQLITE"); // where it holds connection            
                  m_db.setDatabaseName(m_pathDb);            
              }
              bool post_exists = getPaths().existsDb(); // post check if it created it
                      
              if (!(opened = m_db.isOpen()))        
                  opened = m_db.open();            
          
              if (!opened)
              {
                  qDebug() << "Error: connection with database failed";
              }
              else
              {
                  rc = true;
                  qDebug() << "Database: connection ok";
                  if (!pre_exists && post_exists)
                  {
                      m_created = true;                
                  }
              }
              return rc;
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 2 Dec 2021, 07:45 last edited by
          #4

          @SocketSackett said in Sqlite DB is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path:

          if (!pre_exists || (pre_exists && !m_db.isValid()))
          {
          m_db = QSqlDatabase::addDatabase("QSQLITE"); // where it holds connection
          m_db.setDatabaseName(m_pathDb);
          }

          Did you make sure you enter this if block?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply 2 Dec 2021, 17:29
          1
          • S SocketSackett
            2 Dec 2021, 02:12

            It didn't let me finish typing. The once every 10 mins is a bit controlling! This is a RW database.

            The funny thing is I had this was working before I added in DB creation by code instead of the tool, but something changed somewhere, perhaps an environmental change. But when I use the Sqlite tools, I see it created the DB file, dataman.db, it says the DB is a "transient in-memory database". The file is created on disk, but somewhere in the bowels of libraries it is not creating it properly. It is quite befuddling.

            My source is pretty simple:

            bool MdDatabaseManager::connectDb()
            {
            bool rc = false;
            bool opened = false;
            bool pre_exists = getPaths().existsDb();
            m_pathDb = getPaths().makeDbPath();

                if (!pre_exists || (pre_exists && !m_db.isValid()))
                {
                    m_db = QSqlDatabase::addDatabase("QSQLITE"); // where it holds connection            
                    m_db.setDatabaseName(m_pathDb);            
                }
                bool post_exists = getPaths().existsDb(); // post check if it created it
                        
                if (!(opened = m_db.isOpen()))        
                    opened = m_db.open();            
            
                if (!opened)
                {
                    qDebug() << "Error: connection with database failed";
                }
                else
                {
                    rc = true;
                    qDebug() << "Database: connection ok";
                    if (!pre_exists && post_exists)
                    {
                        m_created = true;                
                    }
                }
                return rc;
            
            P Offline
            P Offline
            Pl45m4
            wrote on 2 Dec 2021, 13:16 last edited by Pl45m4 12 Feb 2021, 13:18
            #5

            @SocketSackett said in Sqlite DB is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path:

            The once every 10 mins is a bit controlling!

            Spam / Bot protection (for new users only, once your amount of posts and your rep increases, it's gone, I guess).

            Also, it's not recommended to have the DB connection laying around as member of some class. If you need to get your connection, better use QSqlDatabase::database().


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            S 1 Reply Last reply 2 Dec 2021, 18:31
            0
            • J jsulm
              2 Dec 2021, 07:45

              @SocketSackett said in Sqlite DB is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path:

              if (!pre_exists || (pre_exists && !m_db.isValid()))
              {
              m_db = QSqlDatabase::addDatabase("QSQLITE"); // where it holds connection
              m_db.setDatabaseName(m_pathDb);
              }

              Did you make sure you enter this if block?

              S Offline
              S Offline
              SocketSackett
              wrote on 2 Dec 2021, 17:29 last edited by
              #6

              @jsulm : Yes: My code is exactly like that.

              JonBJ 1 Reply Last reply 2 Dec 2021, 17:38
              0
              • S SocketSackett
                2 Dec 2021, 17:29

                @jsulm : Yes: My code is exactly like that.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 2 Dec 2021, 17:38 last edited by
                #7

                @SocketSackett
                @jsulm is asking for proof that area of code gets executed. Add this line into the if:

                qDebug() << pre_exists << m_pathDb;
                

                and show the result, please.

                S 1 Reply Last reply 2 Dec 2021, 20:59
                0
                • P Pl45m4
                  2 Dec 2021, 01:50

                  @SocketSackett

                  Please show what you did exactly.
                  If you specify a *.db file, it should be there, as long as you don't face any errors while opening the DB connection.

                  For the QSQLITE driver, if the database name specified does not exist, then it will create the file for you unless the QSQLITE_OPEN_READONLY option is set.

                  Additionally, name can be set to ":memory:" which will create a temporary database which is only available for the lifetime of the application.

                  (from: https://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName)

                  S Offline
                  S Offline
                  SocketSackett
                  wrote on 2 Dec 2021, 17:54 last edited by
                  #8

                  @Pl45m4 : It is a RW-DB so I haven't set the QSQLITE_OPEN_READONLY at all. I'm using the file path for the DB name.

                  1 Reply Last reply
                  0
                  • P Pl45m4
                    2 Dec 2021, 13:16

                    @SocketSackett said in Sqlite DB is making an in-memory DB instead of a full file DB even though .setDatabaseName is using a Path:

                    The once every 10 mins is a bit controlling!

                    Spam / Bot protection (for new users only, once your amount of posts and your rep increases, it's gone, I guess).

                    Also, it's not recommended to have the DB connection laying around as member of some class. If you need to get your connection, better use QSqlDatabase::database().

                    S Offline
                    S Offline
                    SocketSackett
                    wrote on 2 Dec 2021, 18:31 last edited by
                    #9

                    @Pl45m4: Thank you for the suggestion. I've applied your suggestion, but I still have the error. But I'm keeping the suggestion in my code because it makes sense. Thx.

                    1 Reply Last reply
                    0
                    • JonBJ JonB
                      2 Dec 2021, 17:38

                      @SocketSackett
                      @jsulm is asking for proof that area of code gets executed. Add this line into the if:

                      qDebug() << pre_exists << m_pathDb;
                      

                      and show the result, please.

                      S Offline
                      S Offline
                      SocketSackett
                      wrote on 2 Dec 2021, 20:59 last edited by
                      #10

                      @JonB : As I said you see it do it the debugger.

                      S 1 Reply Last reply 2 Dec 2021, 21:11
                      0
                      • S SocketSackett
                        2 Dec 2021, 20:59

                        @JonB : As I said you see it do it the debugger.

                        S Offline
                        S Offline
                        SocketSackett
                        wrote on 2 Dec 2021, 21:11 last edited by
                        #11

                        @SocketSackett DB Path : "D://Bennubio//dataman//data_manager.db" , Pre-exists: true

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SocketSackett
                          wrote on 2 Dec 2021, 22:55 last edited by
                          #12

                          I'm not sure what all fixed this but the approach of if (!pre_exists || (pre_exists && !m_db.isValid())) is a good check to avoid duplicate connection. Before that in a close error I was getting a message that it wasn't closing on the duplicate connection. On Stack Overflow, I found the suggestion to do:

                          S 1 Reply Last reply 2 Dec 2021, 23:20
                          0
                          • S SocketSackett
                            2 Dec 2021, 22:55

                            I'm not sure what all fixed this but the approach of if (!pre_exists || (pre_exists && !m_db.isValid())) is a good check to avoid duplicate connection. Before that in a close error I was getting a message that it wasn't closing on the duplicate connection. On Stack Overflow, I found the suggestion to do:

                            S Offline
                            S Offline
                            SocketSackett
                            wrote on 2 Dec 2021, 23:20 last edited by
                            #13

                            @SocketSackett Continuing ... I found the suggestion on Stack Overflow to use: db = QSqlDatabase(); db.removeDatabase(cnxn); after the close was a way to fix the duplicate connection close error. This is wrong. Doing this will not allow the work done to be updated. I removed that, & did a straight DB close, & it is updating once again. Checking for pre-existence of DB path with the QSqlDatabase::database.isValid() will suffice & is a good approach.

                            1 Reply Last reply
                            0

                            1/13

                            2 Dec 2021, 01:46

                            • Login

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