[Resolved] QSqlDatabase how to use Pointer?



  • I trying to use Singleton Pattern on this Class (QSqlDatabase) and I could not to that.
    I make this code, and the compiler gives me an error:

    @
    static QSqlDatabase* getDatabase()
    {
    if (database == 0)
    {
    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName( QDir::homePath() + QDir::separator() + "inoveDB.db3");
    }
    return database;
    }
    @

    Error:

    @
    .../InoveBEV/mainwindow.cpp:14: In file included from ../InoveBEV/mainwindow.cpp:14:0:
    .../InoveBEV/singletonsession.h:-1: In static member function 'static QSqlDatabase* SingletonSession::getDatabase()':
    .../InoveBEV/singletonsession.h:40: error: cannot convert 'QSqlDatabase' to 'QSqlDatabase*' in assignment
    .../InoveBEV/singletonsession.h:41: error: request for member 'setDatabaseName' in 'SingletonSession::database', which is of non-class type 'QSqlDatabase*'
    @

    How can I make a singleton to QOSqlDataBase?!
    I will look for examples and other stuff to help me fix this. Thanks;...



  • Take a look to "QSqlDatabase":http://doc.qt.nokia.com/4.7-snapshot/qsqldatabase.html#addDatabase
    This function return an object no pointer.



  • Your member will have to be of type <code>QSqlDatabase</code>, not <code>QSqlDatabase*</code>; use <code>database.isValid()</code> instead of <code>database == 0</code> and <code>return &database</code> instead of <code>return database</code> then.

    QSqlDatabase can be generally used as value-type as well.

    Another option is to use <code>database = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"))</code>, but you will have to make sure you manually delete your object.

    Do not take the address of a temporary created in <code>getDatabase()</code> and store it in <code>database</code>. This won't work.



  • There is simply no need to create something like a singleton or a global QSqlDatabase. QSqlDatabase already provides the needed functionality for you, as you can create an instance (really cheaply) based on just a name you pass in when creating it. Look at the static member functions of QSqlDatabase.



  • I make some changes on my Singleton and make this:

    @
    static QSqlDatabase getDatabase()
    {
    QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName( QDir::homePath() + QDir::separator() + "inoveDB.db3");
    return database;
    }
    @

    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.



  • have a look at "this":http://qt-project.org/forums/viewthread/18722/:
    and "this":http://qt-project.org/forums/viewthread/18604/. The last one is already your own thread. Why do you start over again?



  • I think I fix my problem using this code:

    @
    static QSqlDatabase getDatabase()
    {
    QSqlDatabase database = QSqlDatabase::database(getConnectionName());
    if (! database.isValid())
    {
    database = QSqlDatabase::addDatabase("QSQLITE", getConnectionName());
    database.setDatabaseName( getDataBaseName() );
    }
    return database;
    }
    static QString getDataBaseName()
    {
    return QDir::homePath() + QDir::separator() + "inoveDB_SAT.db3";
    }
    static QString getConnectionName()
    {
    return "inoveBEV_SAT";
    }
    @

    What do you guys think about this code?
    For me appears to works fine!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.