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. Accessing in memory SQLite databse
Forum Updated to NodeBB v4.3 + New Features

Accessing in memory SQLite databse

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 591 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.
  • A Offline
    A Offline
    abhic
    wrote on last edited by abhic
    #1

    I have integrated sqlite3.* files in our application and am currently using it to create in memory database . All this is being done outside Qt.
    Now within same process, I want to access that in memory database to display some data in Qt GUI. Is this kind of flow supported/recommended?

    Christian EhrlicherC 1 Reply Last reply
    0
    • A abhic

      I have integrated sqlite3.* files in our application and am currently using it to create in memory database . All this is being done outside Qt.
      Now within same process, I want to access that in memory database to display some data in Qt GUI. Is this kind of flow supported/recommended?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @abhic said in Accessing in memory SQLite databse:

      Is this kind of flow supported/recommended?

      If sqlite itself supports opening an in-memory database twice within one process then I don't see why it should not work. But that's out of control of Qt.

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

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Please go through the SQLite documentation about that matter. There several nuances that come with its handling.

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

        A 1 Reply Last reply
        1
        • A Offline
          A Offline
          abhic
          wrote on last edited by
          #4

          Thanks everyone for your responses. I will give it a try

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi and welcome to devnet,

            Please go through the SQLite documentation about that matter. There several nuances that come with its handling.

            A Offline
            A Offline
            abhic
            wrote on last edited by abhic
            #5

            @SGaist I have tried going through it, I am new to this. It seems that usage of shared cache is discouraged. But so far it seems using shared cache is only way to access same in memory db from 2 places within one process

            I have not been able to share in memory database even with this.

            QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
            db.setConnectOptions("QSQLITE_OPEN_URI;QSQLITE_ENABLE_SHARED_CACHE");
            db.setDatabaseName("file::memory:");
            db.open();

            and outside Qt
            std::string dbFileName = "file::memory:?cache=shared";
            bool readOnly = false;
            const int roFlag = readOnly ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
            const int status = sqlite3_open_v2(dbFileName.c_str(), &_db, (roFlag|SQLITE_OPEN_FULLMUTEX|SQLITE_OPEN_URI|SQLITE_OPEN_SHAREDCACHE), NULL);

            A 1 Reply Last reply
            0
            • A abhic

              @SGaist I have tried going through it, I am new to this. It seems that usage of shared cache is discouraged. But so far it seems using shared cache is only way to access same in memory db from 2 places within one process

              I have not been able to share in memory database even with this.

              QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
              db.setConnectOptions("QSQLITE_OPEN_URI;QSQLITE_ENABLE_SHARED_CACHE");
              db.setDatabaseName("file::memory:");
              db.open();

              and outside Qt
              std::string dbFileName = "file::memory:?cache=shared";
              bool readOnly = false;
              const int roFlag = readOnly ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
              const int status = sqlite3_open_v2(dbFileName.c_str(), &_db, (roFlag|SQLITE_OPEN_FULLMUTEX|SQLITE_OPEN_URI|SQLITE_OPEN_SHAREDCACHE), NULL);

              A Offline
              A Offline
              abhic
              wrote on last edited by
              #6

              Is there something missing here? Also is it possible in Qt to use an sqlite3* pointer to existing connection opened outside Qt?

              Christian EhrlicherC C 2 Replies Last reply
              0
              • A abhic

                Is there something missing here? Also is it possible in Qt to use an sqlite3* pointer to existing connection opened outside Qt?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @abhic said in Accessing in memory SQLite databse:

                Also is it possible in Qt to use an sqlite3* pointer to existing connection opened outside Qt?

                Yes, but what should this help - https://doc.qt.io/qt-6/qsqldriver.html#handle

                Use a file instead in-memory.

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

                A 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @abhic said in Accessing in memory SQLite databse:

                  Also is it possible in Qt to use an sqlite3* pointer to existing connection opened outside Qt?

                  Yes, but what should this help - https://doc.qt.io/qt-6/qsqldriver.html#handle

                  Use a file instead in-memory.

                  A Offline
                  A Offline
                  abhic
                  wrote on last edited by abhic
                  #8

                  @Christian-Ehrlicher Thanks for your response.
                  File based DB is resulting in high runtime. I am exploring in memory DB to get around that. In memory DB is populated outside Qt using sqlite C APIs. Now I need to access that in memory DB inside Qt also in read only mode. I have not been able to do that so far. Below code is also not working

                  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                  db.setConnectOptions("QSQLITE_OPEN_URI;QSQLITE_ENABLE_SHARED_CACHE");
                  db.setDatabaseName("file::memory:");
                  db.open();

                  and outside Qt
                  std::string dbFileName = "file::memory:?cache=shared";
                  bool readOnly = false;
                  const int roFlag = readOnly ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
                  const int status = sqlite3_open_v2(dbFileName.c_str(), &_db, (roFlag|SQLITE_OPEN_FULLMUTEX|SQLITE_OPEN_URI|SQLITE_OPEN_SHAREDCACHE), NULL);

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Simply test it with two c++ only ones. This is imo nothing Qt specific. Also I don't see anything in the Sqlite code which could allow your requirement.
                    Use a proper db if you want proper db functionality.

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

                    1 Reply Last reply
                    0
                    • A abhic

                      Is there something missing here? Also is it possible in Qt to use an sqlite3* pointer to existing connection opened outside Qt?

                      C Offline
                      C Offline
                      ChrisW67
                      wrote on last edited by
                      #10

                      @abhic Some possibilities:

                      • Sqlite was built with this SQLITE_OMIT_SHARED_CACHE option. Not sure: not looked at the relevant source.
                      • Maybe the shared in-memory database needs to be accessed by the same Sqlite library (I do not know how the rendezvous works). At the moment the Qt plugin is probably using built-in code, while the "outside Qt" code may be using either a system library or its own built-in Sqlite.
                      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