Results 1 to 8 of 8

Thread: Quick Bindvalue Question

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Quick Bindvalue Question

    I have a query formulated as follows:
    Qt Code:
    1. insert into table (col1, col2, col3) values ("uniquething", :item1, :item2),("uniquething2", :item1, :item2),("uniquething3", :item1, :item2)
    To copy to clipboard, switch view to plain text mode 

    I use bindvalue as follows

    Qt Code:
    1. query.bindValue(":item1", "something");
    2. query.bindValue(":item2", "something2");
    To copy to clipboard, switch view to plain text mode 

    When the code executes the query, the result in the database is:

    Qt Code:
    1. col1 | col2 | col3
    2. -----------------------------------------------------
    3. uniquething | null | null
    4. uniquething2 | null | null
    5. uniquething3 | something | something2
    To copy to clipboard, switch view to plain text mode 

    so it seems that if there are multiple of the same placeholder, bindValue only replaces 1 of them, and leaves the others as null. Is this a bug or is it by design? and is there a way to bind a value to multiple placeholders, other than the obvious of naming all of them uniquely?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Quick Bindvalue Question

    Hi, I don't know if it is a bug or a design thing but I would do it this way:
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("insert into table (col1, col2, col3) values (:uniquething, :item1, :item2)");
    3. query.bindValue(":item1", "something");
    4. query.bindValue(":item2", "something2");
    5.  
    6. query.bindValue(":uniquething", "uniquething");
    7. query.exec();
    8.  
    9. query.bindValue(":uniquething", "uniquething2");
    10. query.exec();
    11.  
    12. query.bindValue(":uniquething", "uniquething3");
    13. query.exec();
    To copy to clipboard, switch view to plain text mode 

    Lykurg

  3. #3
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Quick Bindvalue Question

    Yeah, I could do it that way, but each exec() call causes another query to happen, which means less performance. For example, if I perform it this way:
    Qt Code:
    1. insert into table (col1, col2, col3) values ("uniquething", :item1, :item2),("uniquething2", :item1, :item2),("uniquething3", :item1, :item2)
    To copy to clipboard, switch view to plain text mode 
    then it is sent to the database one time, and executed all in one shot.

    If I do it the other way, then it will be sent as 3 seperate queries, which on a high latency connection or high volume server will cause more overhead.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Quick Bindvalue Question

    Quote Originally Posted by tpf80 View Post
    If I do it the other way, then it will be sent as 3 seperate queries, which on a high latency connection or high volume server will cause more overhead.
    That's right. If your database can handle transactions use this and the performance should be almost equal.

    A look to the sources about the original problem leaves me unwise (Too many d->)

  5. #5
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Quick Bindvalue Question

    Well, here is how I ended up working around it, although it still is strange that bindValue doesn't replace all of the same placeholders with the same thing if you reference them by name.

    Qt Code:
    1. querystring = "insert into table (col1, col2, col3) values ";
    2. for (int i = 0; i < uniquevaluelist.size(); ++i) {
    3.  
    4. querystring += "(:item1, :item2, :item3)";
    5.  
    6. if (i == uniquevaluelist.size() - 1) {
    7.  
    8. } else {
    9. querystring += ", ";
    10. }
    11. }
    12. query.prepare(querystring);
    13.  
    14. int j = 0;
    15. for (int i = 0; i < uniquevaluelist.size(); ++i) {
    16. j = i * 3;
    17. query.bindValue((j + 0), uniquevaluelist.at(i));
    18. query.bindValue((j + 1), "some not unique value");
    19. query.bindValue((j + 2), "some other not unique value");
    20.  
    21. }
    22.  
    23. query.exec();
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Quick Bindvalue Question

    Hi,

    some thougths on your code:
    • :item2 and :item3 are constant, so is it possible to add the values to default values on your Sql database? Then you could leave them blank.
    • as an optimization of your code (first loop):
      Qt Code:
      1. // not checked: uniquevaluelist.size() != 0
      2. querystring = "insert into table (col1, col2, col3) values (:item1, :item2, :item3)";
      3. for (int i = 1; i < uniquevaluelist.size(); ++i)
      4. querystring += ", (:item1, :item2, :item3)";
      To copy to clipboard, switch view to plain text mode 


    Lykurg

  7. #7
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Quick Bindvalue Question

    They are constant on a "per batch" basis, but not on a "per query" basis. ( and in the real code it does check the size of the list to make sure its not 0 ).

    The reason for the performance concern is that I may want to enable remote access to the database through SSH. My testing so far has shown this to not be a good method to access the database.

    For example, each time a query happens, due to latency in the connection, it could take around 1-2 seconds for the result to come back, regardless of the size of the result. For example, 7 queries might take 14 seconds for a result to come back even with a minimal amount of data in them, but a single query, even with a result 100 times bigger than the 7 would come back in only 1 second.

    So, in essence, I need to do 2 things:

    First, I need to make sure that I get the most bang for each query possible, to reduce the effect of latency.

    Secondly, I need to figure out if there is a better way to connect, to where the latency issue wouldn't be as much of a problem, perhaps caching data and using a worker thread to do the database work, or making some kind of "middleman" server between the database and the client app.

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Quick Bindvalue Question

    Ha,

    have a look at this Task Tracker. There was your Problem, but the suggestion was rejected, why ever. Unfortunately there is no description for the reason...


    Lykurg

Similar Threads

  1. Quick question: infinite double?
    By Raccoon29 in forum Qt Programming
    Replies: 2
    Last Post: 17th October 2008, 15:05
  2. Quick question about filtering item model
    By maverick_pol in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2007, 16:29
  3. Quick RegExp question
    By stealth86 in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2007, 08:23
  4. Quick spacing question
    By bruccutler in forum Newbie
    Replies: 4
    Last Post: 8th March 2007, 20:46
  5. Quick question regarding abstract classes and DLLs
    By durbrak in forum Qt Programming
    Replies: 1
    Last Post: 8th February 2007, 21:32

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.