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()
{
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
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[])
{
if (!createConnection())
return 1;
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks