szisziszilvi
17th June 2011, 11: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. :(
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. :(