Unsolved is that the best way to have a connection to database in the software
-
Hi , I've this function in a class called MySqlDatabase
bool MySqlDatabase::ConnectDB(){ MySqlDB = QSqlDatabase::database(); MySqlDB = QSqlDatabase::addDatabase("QMYSQL", "FirstConnection"); MySqlDB.setHostName("localhost") ; MySqlDB.setUserName("root"); MySqlDB.setPassword("mostafa"); MySqlDB.setDatabaseName("Clinic_Database"); MySqlDB.open() ; if(MySqlDB.open()) { QMessageBox::about(0,"Result","db Opned"); return true ; } else { qDebug() << "Error in opneing " << MySqlDB.lastError().text(); return false; }
and when i want to use this class to connect to db i include it then use the object like that
.h of the class that i wanna use this connction in itMySqlDatabase connect ;
.cpp
connect.ConnectDB(); QSqlQuery * qry=new QSqlQuery(connect.MySqlDB);
so is that an efficient way to use it ? or make the bool function of connection in main.cpp file ? .. Thanks in advance
-
Hi,
First thing: don't call open on your database twice.
Then, do you have several different connection to handle ? If not, then there's no need for "FirstConnection". A default connection will be created and can be used through out your application.Last but not least, as shown and explain in the QSqlDatabase documentation, don't keep QSqlDatabase objects around. When you need one from a specific connection use QSqlDatabase::database.
-
@SGaist said in is that the best way to have a connection to database in the software:
Hi,
First thing: don't call open on your database twice.
Then, do you have several different connection to handle ? If not, then there's no need for "FirstConnection". A default connection will be created and can be used through out your application.Last but not least, as shown and explain in the QSqlDatabase documentation, don't keep QSqlDatabase objects around. When you need one from a specific connection use QSqlDatabase::database.
so should i delete this class and rewrite the function of connection in the classes that need a connection to database to prevent QSqlDatabase objects around
for example : i've 5 classes and a lot of functions so should i write the connection in every class and use the function of connection or make one single function in a class and include this class and use his object ..and pls which cases should i use MultiConnection of database e.g .. "FirstConnction" , "SecConnection"
-
Nothing wrong with a database manager class.
Which cases ? If you want to do multithreading access to the database.
-
@SGaist
but you saying it's not popular to make QSqlDatabase objects around .. so what should i do in this case coz i'll include MySqlDatabase class in 5 classes and ofcourse i'll use the objects in every functionand if i used multithreading access to database .. will queries and connections handle at Once or they will be queued ?
-
Yes I did, however there's nothing wrong with having class to help setup the various connection.
-
@SGaist
Thank you , but pls i've last inquiry about MemoryLeaks .. should i destroy every thing i use in the class in the Heap .. when the class it self calling the destructor e.g."ClassName"::~ClassName()
or he will destroy every thing automaticly and is there any tools help me to discover MemoryLeaks in QT or seeing what's going on in the Heap
-
It depends on what you have as member variables your class.
You can use tools like Valgrind to check for memory leaks.
-
@MostafaEzzat said in is that the best way to have a connection to database in the software:
and if i used multithreading access to database .. will queries and connections handle at Once or they will be queued ?
You need to open a separate DB connection for every thread - see https://doc.qt.io/qt-5/threads-modules.html#threads-and-the-sql-module