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. Different directory for different SQL Databases
Forum Updated to NodeBB v4.3 + New Features

Different directory for different SQL Databases

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 781 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by bogong
    #1

    Hello all!
    I've got an issue to manage few databases under SQlite in one Qt Application. Is there any way to store this databases in separate directory and by demand connect that DB that I need? I've been reading https://doc.qt.io/qt-5/qsqldatabase.html but I've not found anything. I need one active db at one time but from different location on device.

    JonBJ 1 Reply Last reply
    0
    • KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @bogong There is not problem to do this

      // Define 1st SQLite DB connection
      auto db1 = QSqlDatabase::addDatabase("QSQLITE", "FirstDB");
      db1.setDatabaseName(<path_to _SQLite_DB_file>);
      
      // Define 2nd SQLite DB connection
      auto db2 = QSqlDatabase::addDatabase("QSQLITE", "SecondDB");
      db2.setDatabaseName(<path_to _SQLite_DB_file>);
      
      ...
      
      // open connection to 1st DB
      auto db = QSqlDatabase::database("FirstDB");
      
      
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      B 2 Replies Last reply
      5
      • B bogong

        Hello all!
        I've got an issue to manage few databases under SQlite in one Qt Application. Is there any way to store this databases in separate directory and by demand connect that DB that I need? I've been reading https://doc.qt.io/qt-5/qsqldatabase.html but I've not found anything. I need one active db at one time but from different location on device.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @bogong

        • I've not used SQLite, but I assume you can arrange for databases to be stored in particular directories by specifying the necessary path to https://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName.

        • To change which of multiple opened database you want to use for an operation:

        If you create multiple database connections, specify a unique connection name for each one, when you call addDatabase(). Use database() with a connection name to get that connection.

        1 Reply Last reply
        2
        • KroMignonK KroMignon

          @bogong There is not problem to do this

          // Define 1st SQLite DB connection
          auto db1 = QSqlDatabase::addDatabase("QSQLITE", "FirstDB");
          db1.setDatabaseName(<path_to _SQLite_DB_file>);
          
          // Define 2nd SQLite DB connection
          auto db2 = QSqlDatabase::addDatabase("QSQLITE", "SecondDB");
          db2.setDatabaseName(<path_to _SQLite_DB_file>);
          
          ...
          
          // open connection to 1st DB
          auto db = QSqlDatabase::database("FirstDB");
          
          
          
          B Offline
          B Offline
          bogong
          wrote on last edited by
          #4

          @KroMignon Will they be located in different directories? For me location of DB - critical. The matter is in location of DB.

          JonBJ 1 Reply Last reply
          0
          • B bogong

            @KroMignon Will they be located in different directories? For me location of DB - critical. The matter is in location of DB.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @bogong

            arrange for databases to be stored in particular directories by specifying the necessary path to https://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName.

            So have you tried it? Should take 1 minute.

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

              Hi,

              @bogong said in Different directory for different SQL Databases:

              I need one active db at one time but from different location on device.

              Can you clarify that statement ? How are you expecting to use these databases ?

              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
              0
              • KroMignonK KroMignon

                @bogong There is not problem to do this

                // Define 1st SQLite DB connection
                auto db1 = QSqlDatabase::addDatabase("QSQLITE", "FirstDB");
                db1.setDatabaseName(<path_to _SQLite_DB_file>);
                
                // Define 2nd SQLite DB connection
                auto db2 = QSqlDatabase::addDatabase("QSQLITE", "SecondDB");
                db2.setDatabaseName(<path_to _SQLite_DB_file>);
                
                ...
                
                // open connection to 1st DB
                auto db = QSqlDatabase::database("FirstDB");
                
                
                
                B Offline
                B Offline
                bogong
                wrote on last edited by bogong
                #7

                @KroMignon Your example works for me. Thx.

                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