QSqlQuery::exec: database not open
16,624
Solution 1
You need to add qsql dll files for Microsoft Access Driver within your sqldrivers folder
Check following if you are debugging
- check your project settings for dll reference(in .pro file of your project, you have to put QT += sql, if you are using Visual studio integration you check reference in project settings form properties)
- check you Qtdirectory for driver folder and dll's
If you are distributing your app, you just copy sql driver folder also with your executable along with required dlls.
edit your QSqlQuery object binded with database connection
db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb};FIL={MSAccess};DBQ=C:/Users/user/Desktop/Final_Version/db.mdb");
database.open();
QSqlQuery query(db);
query.exec("Select ID from TestId");
while(query.next())
{
TestId = query.value(0).toInt();
}
db.close();
I think, some of this will help you... :)
Solution 2
QSqlQuery *query = new QSqlQuery(db);
I think that can help you! :)
Author by
Nermeeno Alami
Updated on June 04, 2022Comments
-
Nermeeno Alami almost 2 years
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(); //==================================================================================
-
Nermeeno Alami almost 11 yearsThank you very much i will try now but please stay around i may need you ... i was waiting for answer since yesterday
-
Nermeeno Alami almost 11 yearscould you please tell me did you mean by "check you Qtdirectory for driver folder and dll's" and how can i check it
-
imalvare almost 11 yearson your installation path there will be a folder named "sqldrivers" with some dlls for database connection. try to locate it. eg: qsqlite4.dll, qsqlite4d.dll etc. If there is missing dlls it may not working.