Results 1 to 3 of 3

Thread: Why connecting QDatabase with QSqlQuery

  1. #1
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question Why connecting QDatabase with QSqlQuery

    Hi,

    This is not so much a cry for help as for an explanation. I have always use my database/query sentences in the following way:
    Qt Code:
    1. QString app_path = QApplication::applicationDirPath();
    2. QString dbase_path = app_path + "/base.db";
    3.  
    4. QSqlDatabase base = QSqlDatabase::addDatabase("QSQLITE", "db-ident-name");
    5. base.setDatabaseName(dbase_path);
    6. base.database();
    7. base.open();
    8. if(base.isOpen() != true){
    9. qDebug("DB can not be oppened!");
    10. }
    11. else {
    12. // execute query
    13. QSqlQuery sql_string;
    14. sql_string.prepare("SELECT * FROM table");
    15. sql_string.exec();
    16. while ( sql_string.next() ) {
    17. ...
    18. }
    19. }
    20. base.close();
    To copy to clipboard, switch view to plain text mode 
    and it has worked. Well most of the times it did, sometimes it gave me some strange errors and behaviour, but I always thought that was because of some bug in the code. But on the current project this did not worked until I read on this forum to connect QSqlQuery with QDatabase:
    Qt Code:
    1. QSqlQuery sql_string(base);
    To copy to clipboard, switch view to plain text mode 
    After doing that everything worked fine. Why is this connection necessary and why did the code in previous example worked? What is the difference in both calls? Is the second one safer, more exact?

    Thank you, Luka

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why connecting QDatabase with QSqlQuery

    Because qsqlquery in this case uses application's default database. Recently added database may not be the default database. Read carefully the description of the method QSqlDatabase::addDatabase.

    By the way the line number 6 is unnecessary.

  3. The following user says thank you to Lesiok for this useful post:

    omci (26th January 2013)

  4. #3
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Why connecting QDatabase with QSqlQuery

    By quickly looking at the code that produced "strange" behaviour now I can understand, what has happened. The default database was a database with the same tables, but different data. Your explanation was great, thank you. I read QSqlDatabase::addDatabase numerous times, but always searching for other things and failed to understand the obvious.

Similar Threads

  1. Replies: 1
    Last Post: 18th July 2011, 12:12
  2. Using QSqlQuery
    By darkman_dev in forum Newbie
    Replies: 2
    Last Post: 4th February 2011, 21:40
  3. can i use QDatabase in open source
    By md84_xp in forum Qt Programming
    Replies: 1
    Last Post: 28th June 2008, 17:03
  4. what is going on a QSqlQuery?
    By mismael85 in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2008, 13:35
  5. QDatabase::transaction()
    By sunil.thaha in forum Qt Programming
    Replies: 2
    Last Post: 3rd February 2006, 13:15

Tags for this Thread

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.