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 745 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 Online
    B Online
    bogong
    wrote on 12 Mar 2019, 12:19 last edited by bogong 3 Dec 2019, 12:23
    #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.

    J 1 Reply Last reply 12 Mar 2019, 13:22
    0
    • K Offline
      K Offline
      KroMignon
      wrote on 12 Mar 2019, 13:22 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 15 Mar 2019, 18:23
      5
      • B bogong
        12 Mar 2019, 12:19

        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.

        J Offline
        J Offline
        JonB
        wrote on 12 Mar 2019, 13:22 last edited by JonB 3 Dec 2019, 13:23
        #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
        • K KroMignon
          12 Mar 2019, 13:22

          @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 Online
          B Online
          bogong
          wrote on 15 Mar 2019, 18:23 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.

          J 1 Reply Last reply 15 Mar 2019, 20:22
          0
          • B bogong
            15 Mar 2019, 18:23

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

            J Offline
            J Offline
            JonB
            wrote on 15 Mar 2019, 20:22 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 15 Mar 2019, 20:51 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
              • K KroMignon
                12 Mar 2019, 13:22

                @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 Online
                B Online
                bogong
                wrote on 19 Mar 2019, 11:35 last edited by bogong
                #7

                @KroMignon Your example works for me. Thx.

                1 Reply Last reply
                0

                1/7

                12 Mar 2019, 12:19

                • Login

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