Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Resolved] QSqlDatabase how to use Pointer?
QtWS25 Last Chance

[Resolved] QSqlDatabase how to use Pointer?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 8.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dcbasso
    wrote on last edited by
    #1

    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;...

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Thanatos.jsse
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dcbasso
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              franku
              wrote on last edited by
              #6

              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?

              This, Jen, is the internet.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dcbasso
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved