Understanding "Connecting to Databases"
Hi
I'm trying to better undestand how the connection to a mysal database works(http://qt-project.org/doc/qt-5.0/qts...onnecting.html).
Ther is something i'm missing about removing a connection i still miss.
Code:
#include <QSqlDatabase>
{
db->setHostName(newHostName);
db->setDatabaseName(newDatabase);
db->setUserName(newUserName);
db->setPassword(newPassword);
return db;
}
int main()
{
//QCoreApplication a(argc, argv);
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."
Re: Understanding "Connecting to Databases"
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:
and this is correct:
Re: Understanding "Connecting to Databases"
Quote:
You are not deleting the connection object anywhere
Grrr ... i forgot :o
Quote:
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:
Code:
#include <QSqlDatabase>
{
db->setHostName(newHostName);
db->setDatabaseName(newDatabase);
db->setUserName(newUserName);
db->setPassword(newPassword);
return db;
}
int main()
{
bool dbStatus = false;
for(int i=0; i<6;i++)
{
if(dbStatus)
{
db->close();
delete db;
dbStatus = false;
}
else
{
db = connection("localhost", "db01", "user1", "123");
db->open();
dbStatus = true;
}
}
}
Re: Understanding "Connecting to Databases"
Quote:
Originally Posted by
graciano
I was thinking of an on/off button in the UI.
This doesn't mean you have to use the new operator:
Code:
#include <QSqlDatabase>
{
db.setHostName(newHostName);
db.setDatabaseName(newDatabase);
db.setUserName(newUserName);
db.setPassword(newPassword);
return db;
}
int main()
{
bool dbStatus = false;
for(int i=0; i<6;i++)
{
if(dbStatus)
{
db.close();
dbStatus = false;
}
else
{
db = connection("localhost", "db01", "user1", "123");
db.open();
dbStatus = true;
}
}
}