Results 1 to 6 of 6

Thread: QSqlQuery prepared statements proper usage

  1. #1
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QSqlQuery prepared statements proper usage

    I'm trying to figure out what is the proper way to use prepared statements with QSqlQuery. The docs are not very specific on this subject.

    Qt Code:
    1. void select(const QSqlDatabase &database) {
    2. QSqlQuery query(database);
    3. query.prepare("SELECT * FROM theUniverse WHERE planet = :planet");
    4. query.bindValue(":planet", "earth");
    5. query.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 
    So will this snippet create a permanent prepared statement in the connection database? Will this prepared statement persist between calls to select(), i.e. will it be saved when the function returns and QSqlQuery query is disposed?

    Or should I create QSqlQuery on the heap and use the same instance over and over again?

  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: QSqlQuery prepared statements proper usage

    This is not essential if it is prepared or not prepared statement. In Yours example query will be destroyed on exit from procedure. If You want to play with result of query just redefine procedure to :
    Qt Code:
    1. QSqlQuery select(const QSqlDatabase &database) {
    2. QSqlQuery query(database);
    3. query.prepare("SELECT * FROM theUniverse WHERE planet = :planet");
    4. query.bindValue(":planet", "earth");
    5. query.exec();
    6. return query;
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QSqlQuery prepared statements proper usage

    What do you mean not essential?
    In my case I have a bunch of repeating queries that I would like to optimize in some way (arguments differ). I wanted to create a prepared query, and make sure it is parsed only once. How do I make sure that the query is prepared only once in a given connection?

  4. #4
    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: QSqlQuery prepared statements proper usage

    Quote Originally Posted by psih128 View Post
    What do you mean not essential?
    In my case I have a bunch of repeating queries that I would like to optimize in some way (arguments differ).
    I mean that in Yours example query is created on stack and will be destroyed on exit from select() procedure.
    Quote Originally Posted by psih128 View Post
    I wanted to create a prepared query, and make sure it is parsed only once. How do I make sure that the query is prepared only once in a given connection?
    Create one instance of QSqlQuery as global (?), prepare them and bind values in all places where You need.

  5. #5
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: QSqlQuery prepared statements proper usage

    I remember correctly, you have to call prepare between execute calls. I would be cautious about creating a query on the stack of a function and then returning that query. Probably the query copy constructor is smart enough not to make a copy of all the results, but I would check it out. You can do some rudimentary profiling with qdebug and qtime.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlQuery prepared statements proper usage

    Quote Originally Posted by psih128 View Post
    So will this snippet create a permanent prepared statement in the connection database? Will this prepared statement persist between calls to select(), i.e. will it be saved when the function returns and QSqlQuery query is disposed?
    The prepared statements stays valid as a long as the QSQlQuery object stays in existence and the database is connected to stays accessible. Once the QSqlQuery goes out-of-scope is is destroyed (this is C++) taking the data structures that represent the prepared statement and your means of accessing them with it.

    For clarification, a prepared statement is not the same thing as a database procedure, which is persisted in the database and can be accessed by any user of the database.

    Or should I create QSqlQuery on the heap and use the same instance over and over again?
    Yes, but you still need to keep the pointer to it somewhere that persists between calls to select(). If "select()" is part of a class then make the QSqlQuery a member variable. How you do it depends on how the rest of your program looks.

  7. The following user says thank you to ChrisW67 for this useful post:

    psih128 (13th April 2011)

Similar Threads

  1. Proper QList usage
    By space_otter in forum Newbie
    Replies: 5
    Last Post: 22nd June 2010, 06:57
  2. QtSql: execBatch() with SELECT statements
    By herrmarder in forum Qt Programming
    Replies: 2
    Last Post: 28th January 2010, 13:43
  3. Replies: 1
    Last Post: 13th November 2009, 01:45
  4. QTSQLITE, prepared query and bindValue()
    By YaK in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2009, 19:13
  5. reading fprintf statements from console in qt
    By KrishnaKishan in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2007, 10:00

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.