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
fileQstringchange at any time ? Do you handle that case ? -
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
fileQstringchange at any time ? Do you handle that case ? -
In that case, why not just open the database in your main function and be done with it ?
-
No, I mean
mainin 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 ?
-
Are you experiencing that using Qt Creator to start your application ?
-
@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.
-
@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.