Results 1 to 2 of 2

Thread: QSqlQuery Best Practices?

  1. #1
    Join Date
    Jun 2019
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    MacOS X

    Default QSqlQuery Best Practices?

    Hello All,

    I have a program that is running a lot of different SQL queries (mostly SELECT and UPDATE) and I was wondering what the best practice is for creating QSqlQuery objects.
    Specifically: Should I create a separate object for each query, or reuse them?

    For example:
    Qt Code:
    1. QSqlQuery findBook("select title, author from books where id = 1");
    2. //some code...
    3.  
    4. QSqlQuery findMoreBooks("select title, author from books where pages > :pages");
    5. findMoreBooks.bindValue(":pages", 40);
    6. while(findMoreBooks.next()){
    7. //some more code..
    8. }
    9.  
    10. QSqlQuery updateABook("update books set title='foo' where id= :id");
    11.  
    12. QSqlQuery updateMoreBooks("update books set printable=0 where pages > 40");
    To copy to clipboard, switch view to plain text mode 

    Or I could do it like this:
    Qt Code:
    1. QSqlQuery bookQuery;
    2.  
    3. bookQuery.prepare("select title, author from books where id = 1");
    4. bookQuery.exec();
    5. //some code...
    6. bookQuery.prepare("select title, author from books where pages > :pages");
    7. bookQuery.bindValue(":pages", 40);
    8. bookQuery.exec();
    9. while(bookQuery.next(){
    10. //some more code...
    11. }
    12.  
    13. bookQuery.prepare("update books set title='foo' where id=:id");
    14. bookQuery.exec();
    15. bookQuery.prepare("update books set printable=0 where pages > 40");
    16. bookQuery.exec();
    To copy to clipboard, switch view to plain text mode 

    Both seem to work, but I want to make sure I'm doing this the correct way.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,403
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery Best Practices?

    If you run them multiple times then use prepare().

    Depending on the database this could mean that the query is uploaded to the database and parsed once instead of every time.

    For such simply queries there might not be much difference but it is easy enough to do.

    Cheers,
    _

Similar Threads

  1. What are best practices for QSS files?
    By danielvianna in forum Newbie
    Replies: 0
    Last Post: 25th September 2015, 17:26
  2. Replies: 2
    Last Post: 9th April 2013, 09:15
  3. Replies: 1
    Last Post: 18th July 2011, 12:12
  4. Best practices concerning QStandardItemModel
    By tim47 in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2010, 05:29
  5. query about best practices
    By Raajesh in forum Qt Programming
    Replies: 3
    Last Post: 13th June 2008, 18:47

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.