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. Handling database

Handling database

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.3k Views 1 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.
  • orzel1244O Offline
    orzel1244O Offline
    orzel1244
    wrote on last edited by
    #1

    Hello, as first I want to say I've seen a lot of problems on other forums, but usually the problems was on old Qt version, or it's unsloved, or the solution doesn't works for me.
    So basicly, I'm creating chat server, I want to use database, for that I'm creating class called: AccountsDatabase, I want the class to access database, create accounts, remove and etc, the bad thing is, when I call the class few times, it makes duplication of connections, it still works, but I don't think it's correct, I tried using close(), removeDatabase(), and even chacking if database is opened, then closing it. but the debugger says Query is still in use (even If I'll kill every Query instance). It would be easy if I could use pointer with QSqlDatabase, but I can't open database if I'm using pointer. The solution is to use singletron, but I don't want to use it as it's bad pattern. Could someone please provide me info about handling connections, removing it and etc correctly? I already read the doc, but it doesnt works for me :/

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Like already written many times: if you are using only one database connection, just call addDatabase once, it will become the default connection that will be used by everything from the SQL module. If you want to open/close the connection all the time, use QSqlDatabase::database. There's no need to re-create that connection all time nor should you store the "added database" as member variables.

      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
      1
      • orzel1244O Offline
        orzel1244O Offline
        orzel1244
        wrote on last edited by
        #3

        Thanks a lot, you're so helpfull :D

        1 Reply Last reply
        0
        • orzel1244O Offline
          orzel1244O Offline
          orzel1244
          wrote on last edited by
          #4

          There's one more question, when should I call addDatabase? I think I could do it for example in main.cpp one time, but is there any way to check if database is already created?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What do you mean by already created ?

            You seem to be mixing accessing a database and creating a database.

            QSqlDatabase represent a database connection, it doesn't create anything by itself.

            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
            1

            • Login

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