Different directory for different SQL Databases
-
wrote on 12 Mar 2019, 12:19 last edited by bogong 3 Dec 2019, 12:23
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. -
wrote on 12 Mar 2019, 13:22 last edited by
@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");
-
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.wrote on 12 Mar 2019, 13:22 last edited by JonB 3 Dec 2019, 13:23-
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.
-
-
@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");
wrote on 15 Mar 2019, 18:23 last edited by@KroMignon Will they be located in different directories? For me location of DB - critical. The matter is in location of DB.
-
@KroMignon Will they be located in different directories? For me location of DB - critical. The matter is in location of DB.
wrote on 15 Mar 2019, 20:22 last edited byarrange 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.
-
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 ?
-
@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");
wrote on 19 Mar 2019, 11:35 last edited by bogong@KroMignon Your example works for me. Thx.
1/7