Handling database



  • 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 :/


  • Lifetime Qt Champion

    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.



  • Thanks a lot, you're so helpfull :D



  • 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?


  • Lifetime Qt Champion

    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.


Log in to reply
 

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