I am trying to use QSqlQueryModel
in order to retrieve some values from my database like such:
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", "test1");
db.setHostName(Vars::strDbHost);
db.setDatabaseName(Vars::strDbName);
db.setPort(Vars::strDbPort);
db.setUserName(Vars::strDbUsername);
db.setPassword(Vars::strDbPassword);
db.open()
QSqlQueryModel model;
model.setQuery(QString("SELECT * FROM users WHERE login=%2").arg(Vars::strUserLogin));
But I keep getting a QSqlQuery::exec: database not open
error.
Why is this, and how can I correctly use QSqlQueryModel
to retrieve the values I want?
You are calling the wrong version of
setQuery
. This only works with db which have the default name. In your case, you need to callvoid QSqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db)
: