PDA

View Full Version : Understanding "Connecting to Databases"



graciano
29th December 2013, 19:51
Hi

I'm trying to better undestand how the connection to a mysal database works(http://qt-project.org/doc/qt-5.0/qtsql/sql-connecting.html).

Ther is something i'm missing about removing a connection i still miss.


#include <QSqlDatabase>

QSqlDatabase *connection(QString newHostName, QString newDatabase, QString newUserName, QString newPassword)
{
QSqlDatabase *db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL", "connection1"));
db->setHostName(newHostName);
db->setDatabaseName(newDatabase);
db->setUserName(newUserName);
db->setPassword(newPassword);
return db;
}

int main()
{
//QCoreApplication a(argc, argv);
QSqlDatabase *db;
bool dbStatus = false;
for(int i=0; i<2;i++)
{
if(dbStatus)
{
db->close();
db->removeDatabase("connection1");
dbStatus = false;
}
else
{
db = connection("localhost", "db01", "user1", "123");
db->open();
dbStatus = true;
}
}
}


This code loops the connect/disconnect to database.
Why do i get the message every time i remove the database?
"QSqlDatabasePrivate::removeDatabase: connection 'connection1' is still in use, all queries will cease to work."

wysota
29th December 2013, 20:33
You are not deleting the connection object anywhere. First of all there is no reason to use the new operator to create an instance of QSqlDatabase. Second of all a call to removeDatabase() has to occur only if all QSqlDatabase objects pointing to that connection have been deleted (it is not enough to close the connection).

Therefore this is wrong:


void fun() {
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "connection1");
QSqlDatabase::removeDatabase("connection1");
}

and this is correct:


void fun() {
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "connection1");
}
QSqlDatabase::removeDatabase("connection1");
}

graciano
29th December 2013, 21:30
You are not deleting the connection object anywhere

Grrr ... i forgot :o

First of all there is no reason to use the new operator to create an instance of QSqlDatabase.
I was thinking of an on/off button in the UI.

Anyway ... this works fine:


#include <QSqlDatabase>

QSqlDatabase *connection(QString newHostName, QString newDatabase, QString newUserName, QString newPassword)
{
QSqlDatabase *db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL", "connection1"));
db->setHostName(newHostName);
db->setDatabaseName(newDatabase);
db->setUserName(newUserName);
db->setPassword(newPassword);
return db;
}

int main()
{
QSqlDatabase *db;
bool dbStatus = false;
for(int i=0; i<6;i++)
{
if(dbStatus)
{
db->close();
delete db;
QSqlDatabase::removeDatabase("connection1");
dbStatus = false;

}
else
{
db = connection("localhost", "db01", "user1", "123");
db->open();
dbStatus = true;
}
}
}

wysota
29th December 2013, 21:50
I was thinking of an on/off button in the UI.

This doesn't mean you have to use the new operator:


#include <QSqlDatabase>

QSqlDatabase connection(QString newHostName, QString newDatabase, QString newUserName, QString newPassword)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "connection1");
db.setHostName(newHostName);
db.setDatabaseName(newDatabase);
db.setUserName(newUserName);
db.setPassword(newPassword);
return db;
}

int main()
{
QSqlDatabase db;
bool dbStatus = false;
for(int i=0; i<6;i++)
{
if(dbStatus)
{
db.close();
db = QSqlDatabase();
QSqlDatabase::removeDatabase("connection1");
dbStatus = false;

}
else
{
db = connection("localhost", "db01", "user1", "123");
db.open();
dbStatus = true;
}
}
}