PDA

View Full Version : Database Connetions



Atomic_Sheep
21st August 2013, 15:14
Hi guys,

The documentation talks about how to open connections, execute SQL queries, however no further discussion is provided in terms of the processing that happens in the background. When you open the connection to a database db.open, what actually happens/what does that mean? I don't understand how long this connection to the database should last. Does it close at the end of the function i.e. once all the function local variables get pulled off the stack or does it remain active and you're able to just db.exec at will in other functions?

Also, any thoughts on globally declaring a database object?

pkj
21st August 2013, 18:47
If you are not using an embedded db or sqllite, a tcp connection is established at db.open.
A db connection must be closed otherwise it will remain active. The database itself might decide to close a connection after certain timeout if so configured.
Instead of globally declaring a database object, you can re-use QSqlDatabase::database for a default. Or by string. Remember the rules about sharing and passing on QSqlDatabase in threading though.

wysota
27th August 2013, 15:27
When you open the connection to a database db.open, what actually happens/what does that mean? I don't understand how long this connection to the database should last. Does it close at the end of the function i.e. once all the function local variables get pulled off the stack or does it remain active and you're able to just db.exec at will in other functions?
This is all dependent on the underlying SQL driver.


Also, any thoughts on globally declaring a database object?
That's usually a bad idea. You can always retrieve the connection object with a static QSqlDatabase::database() call.