Results 1 to 3 of 3

Thread: Regerding execution of query

  1. #1
    Join Date
    Aug 2008
    Posts
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Regerding execution of query

    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
    Qt Code:
    1. bool creatconnection()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. db.setHostName("bigblue");
    5. db.setDatabaseName("flightdb");
    6. db.setUserName("acarlson");
    7. db.setPassword("1uTbSbAs");
    8. bool ok = db.open();
    9. QSqlQuery.exec(" create table sample(id Integer , Name varchar) ");
    10. retutn true;
    11. }
    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
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "../connection.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. if (!createConnection())
    10. return 1;
    11.  
    12. QSqlQuery.exec(" select * from sample ");
    13.  
    14. return app.exec();
    15. }
    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.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Regerding execution of query

    when you call QSqlDatabase::addDatabase() without passing a name, you create the so called default db. When you then create a QSqlQuerywithout passing a QSqlDatabase, this default db will be used.

  3. #3
    Join Date
    Aug 2008
    Posts
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Regerding execution of query

    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.

Similar Threads

  1. problem while executing SQL query
    By sosanjay in forum Qt Programming
    Replies: 3
    Last Post: 9th October 2009, 12:52
  2. Best way to abort long SQL query?
    By wdezell in forum Newbie
    Replies: 1
    Last Post: 11th September 2009, 22:05
  3. Error executing SELECT query with QSQLITE
    By garfield85 in forum Qt Programming
    Replies: 6
    Last Post: 25th May 2009, 18:05
  4. How to realize multi-threaded database query?
    By bangqianchen in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2008, 07:15
  5. Aborting a query set on a TableModel
    By Quid in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2006, 22:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.