PDA

View Full Version : How does QSqlDatabase work?



szisziszilvi
17th June 2011, 10:20
Hi,
there are some issues I can't understand around the database-handling. This is simply some mistery to me... :)
So now I connect to a database using a function that was created based on an in-built tutorial.


bool createConnection()
{
std::ofstream outf("teszt_connection.txt");
outf << "createConnection\n";

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("myHOst);
db.setPort(1234);
db.setUserName("myUN");
db.setPassword("myPWD");
db.setDatabaseName("myDBN");

if (!db.open()) return false;
else return true;
}
}

Now I don't understand how this can estabilish the connection so the databbase can be accessed from anywhere outside? There is a local variable db, and everything is set on this variable. But I can reach the database out of this function too with QSqlQuery objects and there is no need telling it that there has ever been a QSqlDatabase object (namely db) that was set. On the other hand there might questions rise about the database itself and I cannot "ask" them outside this function because those should be functions of a QSqlDatabase class but the variable is no more accessable.

For example it is impossible to call the function "tables()" in the same scope where createConnection() was called because tables() is for QSqlDatabase instances and the only one, namely db, is available only in the function createConnection(). But how does than the environment know anything then? How can be say more than one databases handled if QSqlQuery does not wait for any database-related identifyer so the command is "sent in the air"? I'm really confused. :(

cincirin
17th June 2011, 10:56
quote from Qt docs:
QSqlDatabase also supports the concept of a default connection, which is the unnamed connection. To create the default connection, don't pass the connection name argument when you call addDatabase(). Subsequently, when you call any static member function that takes the connection name argument, if you don't pass the connection name argument, the default connection is assumed.

wysota
18th June 2011, 00:32
Under the hood there is a static map of connections maintained by Qt. The local variable can go out of scope but its copy is kept in the static map.

falconium
7th July 2011, 18:13
Szia Szilvi!

In the examples so far, we have assumed that the application is using a single database connection. If we want to create multiple connections, we can pass a name as a second argument to addDatabase(). For example:

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", "OTHER");
db.setHostName("saturn.mcmanamy.edu");
db.setDatabaseName("starsdb");
db.setUserName("hilbert");
db.setPassword("ixtapa7");


We can then retrieve a pointer to the QSqlDatabase object by passing the name to QSqlDatabase::database():

QSqlDatabase db = QSqlDatabase::database("OTHER");


To execute queries using the other connection, we pass the QSqlDatabase object to the QSqlQuery constructor:

QSqlQuery query(db);
query.exec("SELECT id FROM artist WHERE name = 'Mando Diao'");


Multiple connections are useful if we want to perform more than one transaction at a time, since each connection can handle only a single active transaction. When we use multiple database connections, we can still have one unnamed connection, and QSqlQuery will use that connection if none is specified.