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. SQLite management of multiple databases
Forum Updated to NodeBB v4.3 + New Features

SQLite management of multiple databases

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 4 Posters 4.8k Views 2 Watching
  • 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.
  • B Binary91
    31 Aug 2022, 10:14

    @jsulm Hi,

    what I meant was the differentiation of "creating" the connection with QSqlDatabase::addDatabase(xxx) in the constructor (and so removeDatabase(xxx) in the destructor) and opening / closing it with the database object's methods db.open() and db.close() each time I need the connection for access to the database... And I would do it that way for any database I have. Creating the connections early and hold it to the end, but the opening/closing of these connections is done repetitively while runtime each time I need access to the database.

    Is that the correct way of use?

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 31 Aug 2022, 10:17 last edited by
    #21

    @Binary91 Opening and closing database connections is expensive. So, if you use the connection often you should open it once and close when your application terminates or you really do not need the connection anymore.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    B 1 Reply Last reply 31 Aug 2022, 10:20
    1
    • J jsulm
      31 Aug 2022, 10:17

      @Binary91 Opening and closing database connections is expensive. So, if you use the connection often you should open it once and close when your application terminates or you really do not need the connection anymore.

      B Offline
      B Offline
      Binary91
      wrote on 31 Aug 2022, 10:20 last edited by Binary91
      #22

      @jsulm Thank you! That is what I wanted to know. So there is no performance leak having one or more connections continously opened while runtime? And it doesn't matter if there were multiple open connections (I don't need that right now, just to know it..) to one database? It can only have one connection direct read/write access at one time point and other commands are queued, right?

      1 Reply Last reply
      0
      • B Binary91
        31 Aug 2022, 08:44

        Hi together,

        sorry for the belated reply.

        Well, I see that I am not the only one who discusses about the right way to deal with that.

        I decided now to have a class called "configMain" and in that class, by calling the constructor while instantiating, I create a connection (addDatabase) and in the destructor I remove the connection (removeDatabase). So the connection stays as long as the mainConfig object is alive (the class that handles the database).

        In the program code, I open and close this connection each time I need access to the database (x.open(), x.close()). I get access to the database connection with the static method QSqlDatabase::database(identifier) everywhere global in the code, what is really well so I dont need (and, as jsulm correctly mentioned) also shouldnt create a member variable of the QSqldatabase object.

        Well, what do you think about that way to deal with it? Good or bad?

        H Offline
        H Offline
        HoMa
        wrote on 31 Aug 2022, 10:23 last edited by
        #23

        @Binary91 said in SQLite management of multiple databases:

        (x.open(), x.close()).

        I don't think so. Do the open() / close() also in your long lived config and only ask for the connection with QSqlDatabase::database(connection) if you need one. I see no reason to do more. I have the impression, that the layout of the QSqlDatabase layout is ment to be used this way.
        Again: The details in the documentation of the opening and closing calls are important.

        1 Reply Last reply
        1
        • B Offline
          B Offline
          Binary91
          wrote on 31 Aug 2022, 10:33 last edited by
          #24

          Ok, alright so I'll do it like recently described right now for all my connections.

          Thank you really much for your support, very great!

          Kind regards,
          Binary

          1 Reply Last reply
          0

          21/24

          31 Aug 2022, 10:17

          • Login

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