PDA

View Full Version : Regerding execution of query



sudheer168
8th November 2009, 07:21
HI ,
I am using QT4.4 with visual studio 2005. My problem is that how the query is going to executes on database. I am having two files ,main.cpp and connection.h

In connection.h I created a connection and opened it.

connection.h



bool creatconnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("bigblue");
db.setDatabaseName("flightdb");
db.setUserName("acarlson");
db.setPassword("1uTbSbAs");
bool ok = db.open();
QSqlQuery.exec(" create table sample(id Integer , Name varchar) ");
retutn true;
}



Now in main.cpp I called this function to open and executed one query
main.cpp



#include <QtGui>

#include "../connection.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

if (!createConnection())
return 1;

QSqlQuery.exec(" select * from sample ");

return app.exec();
}



Every thing is working fine .My doubt is how the query in main.cpp firing on database which I have created in connection.h with out providing the QSqldatabase object. I read the documentation regrading QTSQL module ,which it provides that it call default database. But Here I am not executing query in connection.h ,I am executing in main.cpp ,here how it is able to fire on a particular database in main.cpp.
So I am very much confused about How it is working . Please help me to come out off this confusion.

Please suggest me to solve this problem

Regards,
Sudheer.

caduel
8th November 2009, 09:24
when you call QSqlDatabase::addDatabase() without passing a name, you create the so called default db. When you then create a QSqlQuery without passing a QSqlDatabase, this default db will be used.

sudheer168
9th November 2009, 04:29
Hi ,
Thanks for your kind reply. According to you when I creat a db with out passing a name then the default one will be created and that will be called when I execute the query. But in my code which I posted the db was created as" MYSQL " . and I also didnot understand how the mysql db was calling when I execute the query in main.cpp without passing the db object in the query.

Please suggest me to come out of this confusion.

Regards,
Sudheer.