QSqlQuery::exec: database not open

Nermeeno Alami picture Nermeeno Alami · Jun 1, 2013 · Viewed 15.5k times · Source

I'm working on qt, my database was rightly connected with qt but suddenly i have the following problem every time i debug,,,i i become not able to fetch or to add data from/to the database,,, i don't know whats's the matter but i'm new to qt.

QSqlQuery::exec: database not open

could anybody help please ,,it's an emergency case here the code

db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:/Users/user/Desktop/Final_Version/db.accdb");
db.close();
db.open();
QSqlQuery query;
query.exec("Select ID from TestId");
while(query.next())
{
 TestId = query.value(0).toInt();
}
db.close();
//==================================================================================

Answer

darialarionova picture darialarionova · Jan 28, 2014
QSqlQuery *query = new QSqlQuery(db);

I think that can help you! :)