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. [Solved] QSqlDatabase::database() Not functioning as expected
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved General and Desktop
sql
4 Posts 2 Posters 1.2k 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.
  • S Offline
    S Offline
    Sam2304
    wrote on 1 Jul 2015, 10:40 last edited by Sam2304 7 Feb 2015, 07:52
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 1 Jul 2015, 23:29 last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam2304
        wrote on 2 Jul 2015, 07:52 last edited by
        #3

        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,

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 2 Jul 2015, 21:29 last edited by
          #4

          Working on Windows ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          1/4

          1 Jul 2015, 10:40

          • Login

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