Correct startup of program
-
Hi! :-)
I have implemented a little program with an SQL Lite database connection. My main function looks like this:
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); DBConnection *dbc = DBConnection::getInstance(); //Singleton implementation dbc->openConnection(); delete dbc; return a.exec(); }And there is the problem... the dbc-object will be deleted immediately.
Can someone give me advice how I should implement that correctly?
Thx in advance :-)
bl
-
Hi! :-)
I have implemented a little program with an SQL Lite database connection. My main function looks like this:
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); DBConnection *dbc = DBConnection::getInstance(); //Singleton implementation dbc->openConnection(); delete dbc; return a.exec(); }And there is the problem... the dbc-object will be deleted immediately.
Can someone give me advice how I should implement that correctly?
Thx in advance :-)
bl
@buzz_lightzyear
Well of course, because youdeleteit immediately!If you want to keep the database/connection around for re-use, don't delete it. If it's a singleton implementation you don't even need to keep the variable accessible since you can always call
DBConnection::getInstance()again when you want it.Quite what your
DBConnectionis I do not know. Normally for SQLite you would use Qt'sQSqlDatabaseet al classes. -
Hi! :-)
I have implemented a little program with an SQL Lite database connection. My main function looks like this:
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); DBConnection *dbc = DBConnection::getInstance(); //Singleton implementation dbc->openConnection(); delete dbc; return a.exec(); }And there is the problem... the dbc-object will be deleted immediately.
Can someone give me advice how I should implement that correctly?
Thx in advance :-)
bl
@buzz_lightzyear said in Correct startup of program:
And there is the problem... the dbc-object will be deleted immediately.
Because you open and delete it immediately.
What kind of class is
DBConnection? Do you really need it?
QSqlDatabaseand its static functions wont work for you?Edit:
@JonB :D -
Thx for your answers, I think I have told my problem wrong.
I think my problem is this line:
return a.exec();All stuff I want to do I have to do before this line, also delete my database connection with
delete dbc;Now I have made some changes to my code like this:
a.exec(); delete dbc; return 0;Is this ok to do it this way? Or can that lead to any other problems?
Thank you :-)
-
Thx for your answers, I think I have told my problem wrong.
I think my problem is this line:
return a.exec();All stuff I want to do I have to do before this line, also delete my database connection with
delete dbc;Now I have made some changes to my code like this:
a.exec(); delete dbc; return 0;Is this ok to do it this way? Or can that lead to any other problems?
Thank you :-)
-
Thx for your answers, I think I have told my problem wrong.
I think my problem is this line:
return a.exec();All stuff I want to do I have to do before this line, also delete my database connection with
delete dbc;Now I have made some changes to my code like this:
a.exec(); delete dbc; return 0;Is this ok to do it this way? Or can that lead to any other problems?
Thank you :-)
@buzz_lightzyear said in Correct startup of program:
auto ret = a.exec(); delete dbc; return ret; -
Thank you all for your help! :-)