QSqlDatabase Usage
-
Some quick questions about QSqlDatabase.
Should I be doing a QSqlDatabase::removeDatabase() after every modification of the database? Or is a QSqlDatabase::close() appropriate? Or does the QSqlDatabase falling out of scope do the same?
Do I just do a QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE") once to initialize when needed then a QSqlDatabase::removeDatabase() in the destructor of the program or when everything is complete? Does the connection survive until a QSqlDatabase::removeDatabase() call?
Do I need to call QSqlDatabase::setDatabaseName(name) each time I modify the database or is just a QSqlDatabase db = QSqlDatabase::database() sufficient?
See here for some but not all relevant information.
-
@Crag_Hack
Yes,close()
when it is that you're done. Either your program has a close down where you shut any open resources, or you could do it just before exiting. Whenever you are done with doing SQL stuff.You don't need to do anything after you have done
INSERT
/UPDATE
/DELETE
s.setDatabaseName()
is a one-off operation. You do it just beforeopen()
ing, like you would for a file. https://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName:QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=myaccessfile.mdb"); if (db.open()) { // success! }
-
@Crag_Hack said in QSqlDatabase Usage:
after every modification of the database?
what do you mean with 'modification' here?
-
@Crag_Hack
Yes,close()
when it is that you're done. Either your program has a close down where you shut any open resources, or you could do it just before exiting. Whenever you are done with doing SQL stuff.You don't need to do anything after you have done
INSERT
/UPDATE
/DELETE
s.setDatabaseName()
is a one-off operation. You do it just beforeopen()
ing, like you would for a file. https://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName:QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=myaccessfile.mdb"); if (db.open()) { // success! }