Results 1 to 6 of 6

Thread: QSqlQuery bindValue and SELECT WHERE IN

  1. #1
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default QSqlQuery bindValue and SELECT WHERE IN

    Hi!

    Is it possible to use bindValue with a SELECT statement like:

    Qt Code:
    1. SELECT * FROM stuff WHERE id IN (1, 2, 3, 4)
    To copy to clipboard, switch view to plain text mode 

    I have tried several options, including using a QList<QVariant> with:

    Qt Code:
    1. SELECT * FROM stuff WHERE id IN (:ids)
    To copy to clipboard, switch view to plain text mode 

    but no luck

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery bindValue and SELECT WHERE IN

    No, I don't think so.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2010
    Location
    Opole, PL
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlQuery bindValue and SELECT WHERE IN

    Perhaps You could try using unnamed parameters. In my code I use something like this:

    Qt Code:
    1. Query q("SELECT * FROM tag WHERE tag = ?", connection);
    2. q << name;
    To copy to clipboard, switch view to plain text mode 

    which uses following piece of code:

    Qt Code:
    1. Query &Query::operator <<(QVariant &param)
    2. {
    3. queryObject.bindValue(_paramBound++, param);
    4.  
    5. return *this;
    6. }
    To copy to clipboard, switch view to plain text mode 

    I know this solution is not perfect and has some flaws, but for sure it can be used this way to pass QLists as list of values to bind query parameters.
    Last edited by nephre; 6th November 2010 at 13:13. Reason: Badly formatted citation included
    In the 1960's people took LSD to make the world weird.
    Now the world is weird and people take Prozac to make it normal.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery bindValue and SELECT WHERE IN

    That's not the point here. The point is that you want to be able to bind a list of ids separated by commas to a single placeholder. With your solution you'd still need to know how many items the list has to count placeholders that need to be put in the query.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2010
    Location
    Opole, PL
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlQuery bindValue and SELECT WHERE IN

    Of course, I'm aware of that, it's not a clean solution. Someone could play with parsing SQL query and replacing single unnamed parameter with QList::count() parameters, then binding them all. Question is: is it worth? Because for me, it's dirty for sure.

    Since I don't have much experience with Qt/C++ and SQL programming, so far I never needed to use queries with variable abount of parameters. Maybe some ORM framework would fit here, but I don't know how ORM offer looks in C++ world.
    In the 1960's people took LSD to make the world weird.
    Now the world is weird and people take Prozac to make it normal.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery bindValue and SELECT WHERE IN

    A solution to this concrete problem would probably be to use a subquery returning the list. Something like:
    SQL Code:
    1. SELECT * FROM stuff WHERE id IN (SELECT stuff_id FROM foreigntable WHERE value < :value)
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QSqlQuery and single quotes using bindValue
    By Luc4 in forum Qt Programming
    Replies: 4
    Last Post: 26th September 2010, 23:34
  2. QSqlQuery::bindValue
    By viglu in forum Newbie
    Replies: 3
    Last Post: 29th March 2010, 20:13
  3. QSqlQuery::bindValue() Question
    By kandalf in forum Qt Programming
    Replies: 7
    Last Post: 30th January 2010, 12:14
  4. QSqlQuery, bindValue and Sqlite
    By cydside in forum Qt Programming
    Replies: 4
    Last Post: 5th April 2009, 16:53
  5. QSqlQuery::bindValue problem
    By segfault in forum Qt Programming
    Replies: 6
    Last Post: 11th March 2009, 07:27

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.