Qt Check if there is an open database connection
Good day all.
Im currently using this code in my project to open a mysql database file
..... mydatabase.cpp....
Code:
bool myDatabase::createDatabaseConn()
{
QSettings settings
("ATSTech",
"ats_shopfront");
settings.beginGroup("database");
db.setHostName(settings.value("server").toString());
db.setDatabaseName("dbname");
db.setUserName(settings.value("databaseUsername").toString());
db.setPassword(settings.value("databasePassword").toString());
if (!db.open()) {
//QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
return false;
}
return true;
settings.endGroup();
}
No when i use this from my other pages like so
Code:
myDatabase *getinfo = new myDatabase();
getinfo->createDatabaseConn();
i get the following warinings/errors
Code:
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
Is there any way to in the code at the top to check if there is already a connection and if so use that one instead of removing and creating a new one to the database.
im sure that would make things faster aswell
regards
Donovan Hoare
Re: Qt Check if there is an open database connection
First use addDatabase like this :
Code:
bool myDatabase::createDatabaseConn()
{
QSettings settings
("ATSTech",
"ats_shopfront");
settings.beginGroup("database");
if( db.isValid() )
return true;
db.setHostName(settings.value("server").toString());
db.setDatabaseName("dbname");
db.setUserName(settings.value("databaseUsername").toString());
db.setPassword(settings.value("databasePassword").toString());
if (!db.open()) {
//QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
return false;
}
return true;
settings.endGroup();
}
Re: Qt Check if there is an open database connection
First when adding a database, name the connection:
Then use that name to query for the connection:
Code:
{
// now do some stuff with it
}
else
{
// connection not found, do something
}
Re: Qt Check if there is an open database connection
Spitfire has the answer ;)
Re: Qt Check if there is an open database connection
Quote:
Originally Posted by
Spitfire
First when adding a database, name the connection:
Then use that name to query for the connection:
Code:
{
// now do some stuff with it
}
else
{
// connection not found, do something
}
According to your device i changed my code to this
Code:
QSettings settings
("ATSTech",
"StockManager");
settings.beginGroup("database");
/* if( QSqlDatabase::contains( "StockManagerConn" ) )
{
QSqlDatabase db = QSqlDatabase::database( "StockManagerConn" );
qDebug()<<"Database Open : Using Stockmanager Connection";
return true;
}
else
{*/
db.setHostName(settings.value("server").toString());
db.setDatabaseName("stockmanager");
db.setUserName(settings.value("databaseUsername").toString());
db.setPassword(settings.value("databasePassword").toString());
if (!db.open()) {
return false;
}
qDebug()<<"Database Opened : Started Stockmanager Connection";
return true;
//}
settings.endGroup();
but i started getting this error
Code:
Database Opened : Started Stockmanager Connection
ERROR
: inserting Company Name
QSqlError(-1,
"Driver not loaded",
"Driver not loaded")
As soon as i remove the connection name it works great.
What could i possibly be doing wrong.
Regards
Re: Qt Check if there is an open database connection
You're showing wrong part of your code.
Error is generated by the part that executes a query not sets up the database.
You're probably not using connection name there.
When creating the query you have to specify which connection you want to use:
otherwise it will fall back on the default name and fail.
[Solved] Qt Check if there is an open database connection
Quote:
Originally Posted by
Spitfire
You're showing wrong part of your code.
Error is generated by the part that executes a query not sets up the database.
You're probably not using connection name there.
When creating the query you have to specify which connection you want to use:
otherwise it will fall back on the default name and fail.
Thank you so much you are right.
When declaring my QSqlQuery i changed it to add the connection and it worked great.
You are a star.
Re: [Solved] Qt Check if there is an open database connection
I'm glad I could help :)
Take care!