4

What is the correct way to handle connections for QSqlDatabase?

In my program I am doing it this way:

DatabaseConnector *databaseConnector = 0;
try
{
    databaseConnector = new DatabaseConnector();
    //Do stuff...
    delete databaseConnector;
}
catch(QString e)
{
    delete databaseConnector;
    QMessageBox::information(this,"Error",e);
}

databaseConnector.h

#ifndef DATABASECONNECTOR_H
#define DATABASECONNECTOR_H
#include <QtSql>

class DatabaseConnector
{
public:
    DatabaseConnector();
    DatabaseConnector(QString hostname, QString database, QString user, QString password);
    ~DatabaseConnector();
private:
    QSqlDatabase db;
};

#endif // DATABASECONNECTOR_H

databaseconnector.cpp

#include "databaseconnector.h"
#include <QString>

DatabaseConnector::DatabaseConnector()
{
    QSettings settings;

    db = QSqlDatabase::addDatabase("QIBASE");
    db.setHostName(settings.value("db/host").toString());
    db.setDatabaseName(settings.value("db/name").toString());
    db.setUserName(settings.value("db/user").toString());
    db.setPassword(settings.value("db/pass").toString());

    if(!db.open())
    {
        QString databaseConnectionError = db.lastError().text();
        throw databaseConnectionError;
    }
}

DatabaseConnector::DatabaseConnector(QString hostname, QString database, QString user,     QString password)
{
    db = QSqlDatabase::addDatabase("QIBASE");
    db.setHostName(hostname);
    db.setDatabaseName(database);
    db.setUserName(user);
    db.setPassword(password);
    if(!db.open())
    {
        QString databaseConnectionError = db.lastError().text();
        throw databaseConnectionError;
    }

}

DatabaseConnector::~DatabaseConnector()
{
    db.close();
}

I'm getting error even if I use QSqlDatabase::removeDatabase(db.connectionName());

QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

3 Answers 3

6

Normally you don’t need to open the database connection more than once within your application.

When adding a database, you can name the connection :

QSqlDatabase::addDatabase( "QIBASE", "MyDBConnectionName" );

You can use the name to query for the connection :

if( QSqlDatabase::contains( "MyDBConnectionName" ) )
{
    QSqlDatabase db = QSqlDatabase::database( "MyDBConnectionName" );
    //Do stuff...
}
else
{
    // connection not found, do something
}

Also notice that before calling QSqlDatabase::removeDatabase you should disconnect your database :

db.close();
QSqlDatabase::removeDatabase("MyDBConnectionName");
Sign up to request clarification or add additional context in comments.

1 Comment

I have problem with that, when i add DatabaseConnectionName and remove it in destructor after db.close() i'm getting two errors: QSqlDatabasePrivate::removeDatabase: connection 'fbc' is still in use, all queries will cease to work. and QSqlQuery::prepare: database not open
0

In order to add a new database, you need to give it a name. If you do not give a unique name, the default database is re-used. This is documented in the class reference.

Try:

db = QSqlDatabase::addDatabase("QIBASE", databaseName);

Comments

-2

In main app:

QSqlDatabase db =QSqlDatabase::addDatabase( "QSQLITE");

in second app:

QSqlDatabase db2 =QSqlDatabase::database();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.