Solved db is not open when called the second time
-
One of the thing you should first clean is how you handle your database. Your open statement should go in the same if where you create add the database.
Also, does
fileQstring
change at any time ? Do you handle that case ? -
@SGaist
fileQString doesn't change. I keep using the same db. -
In that case, why not just open the database in your main function and be done with it ?
-
@SGaist
You mean the main function of MainWindow_ -
No, I mean
main
in yourmain.cpp
-
@SGaist
I did the following:#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; QSqlDatabase db; QString fileQstring = "C:/Programming/Projects/FolkFriends_1_0/db.db"; db = QSqlDatabase::addDatabase ("QSQLITE"); db.setDatabaseName (fileQstring); qDebug() << "Connection Display created in connection(). "; bool OK = db.open (); if(OK == true) { qDebug() << "The db (MainWindow) is open!"; } else { qDebug() << "The db (MainWindow) is not open!"; } if(!db.open ()) { qDebug() << "The database (db) is NOT open!" << db.lastError (); } db.open(); w.show(); return a.exec(); }
I still get
The query is NOT active. QSqlError("", "Driver not loaded", "Driver not loaded") -
What version of Qt are you using ?
Do you have write access to that location ?
-
@SGaist
Yes . I have write access. Using Qt 5.7 on Windows 10 -
Are you experiencing that using Qt Creator to start your application ?
-
@SGaist
It starts ok. -
@gabor53
I think the problem is that it first executes function Addview() from mainwindow.cpp. this crates the first half of the messages:
QSqlQuery::exec: database not open
The query is NOT active. QSqlError("", "Driver not loaded", "Driver not loaded")After mainwindow.cpp it executes main.cpp where I create the db connection and open the db. This gives normal messages:
Connection Display created in main.cpp.
The db (MainWindow) is open!Clearly the problem is that it tries to run the query before it opens the db. I just still don't know why it processes mainwindow.cpp first and main.cpp second.
-
-
@jsulm
This worked. Thank you.