PDA

View Full Version : How does one use QSqlQuery pointer to connect to a database?



zim
3rd July 2011, 01:54
How does one connect a database pointer to a sqlquery pointer. For example,


db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
qry = new QSqlQuery(*db);

ChrisW67
3rd July 2011, 10:41
QSqlQuery does not have a constructor taking a QSqlDatabase* so you cannot do what you ask. Also, QSqlDatabase::addDatabase() returns a QSqlDatabase, so there is no need to do anything else to the return value.

What you want to do, after you have read the docs and looked at the examples (which show this pattern over and over) is:


QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/some/file/path");
// you might need credentials for other database types
if (db.open()) {
QSqlQuery qry(db);
qry.prepare(...);
// etc.
}