[Solved] QSqlDatabase::database() Not functioning as expected



  • Hi All,

    I have two classes, one wraps the other. The first 'SQLiteBackend' creates a database connection with:

     QSqlDatabase mDBConnection = QSqlDatabase::addDatabase ("QSQLITE", "CoreData");
    

    Throughout the rest of the class, wherever I need to access the database, for example if I want to add a new table, or execute a query, I use something like this:

    QSqlDatabase mDBConnection = QSqlDatabase::database ("CoreData");
    

    Which works flawlessly (It took me a while to wrap my head around why this works (QT Magic somewhere in the background??), but it works so I'll leave it at that).

    Within 'SQLiteBackend', this mechanism works perfectly. however attempting to resolve the database with ::database in the wrapper class 'CoreData' (the purpose of which is to create a read-only data model for use elsewhere to show data) fails to find the database.

    When I look into this in the debugger, it appears that the above line is parsed correctly, which leaves me wondering why the database connection can't be found.

    Is this the desired QT implementation? Do I need to use AddDatabase again? or should I create a pointer to the database from within the first class and pass it back to the wrapper class?

    I'd appreciate some advice on what the best way to move forward is...

    Note, the reason I have these functions split across two classes is to eventually make the Backend class a plugin, and implement several other database types, with their DB specific functions wrapped into a uniform interface for the rest of the application.

    Look forward to your responses,

    Regards, Sam2304.


  • Lifetime Qt Champion

    Hi,

    Is that wrapper class in another library ? Working in another thread ?



  • Hi,

    It's in the same library, currently in the same thread (though i will be looking at shunting it into a different thread at some point in the future..).

    I appear to have resolved the issue by completely cleaning the shadow build directory and rebuilding from scratch. I still don't quite understand why it works the way it does (and very well it seems!).

    I'm guessing the database instance persists somewhere in the meta-object system. Presumably modelViews connected to the database will update correctly when new data is added etc.?

    Thanks,


  • Lifetime Qt Champion

    Working on Windows ?


Log in to reply
 

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