Results 1 to 7 of 7

Thread: QTSql String Quoteing

  1. #1
    Join Date
    Sep 2006
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTSql String Quoteing

    I recently discovered an issue in an application I have created, where someone can put a ' in an entry field and cause the query to screw up..

    I know in php I have many functions to quote sql properly, but I can't seam to find one included with QSql or even one wrote in c++ for that matter?

    if anyone with experience with this could point me in the right direction, towards such a function I would very much appreciate it :-D

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTSql String Quoteing

    Use QSqlQuery::bindValue() and forget about quotes.

  3. #3
    Join Date
    Sep 2006
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTSql String Quoteing

    well I think thats what I was looking for, do you know of a good tutorial for doing so

    right now I am jsut putting my query into a Qstring then using the exec function to get the query and going from there

    but this looks like I have to use qsql to build the query, probabally a better way of doing it thoguh

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTSql String Quoteing

    Quote Originally Posted by ldsjohn View Post
    well I think thats what I was looking for, do you know of a good tutorial for doing so
    Whole tutorial for one method?

    Qt Code:
    1. q.prepare( "INSERT INTO some_table( some_column ) VALUES ( :some_value )" );
    2. q.bindValue( ":some_value", _someValue );
    3. if( q.exec() ) {
    4. // ok
    5. }
    6. else {
    7. // error
    8. }
    To copy to clipboard, switch view to plain text mode 
    Just watch out for the placeholders --- you'll get interesting results if you forget one bindValue() or make a typo in placeholder name.

    Instead of ":name" you can use "?" (in such case you'll have to specify a number as the first argument for bindValue()).

  5. #5
    Join Date
    Sep 2006
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTSql String Quoteing

    thanks, I am normally not so clueless, long day, I am a one man dev team for a construciton type company and this one problem has gotten me too many phone calls to count today...

    thanks a bunch

  6. #6
    Join Date
    Sep 2006
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTSql String Quoteing

    one other question if you have a moment, can I reuse q, like if I have three querys is there somethign i should do to to clear it, or can I just q.prepare again?

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTSql String Quoteing

    Quote Originally Posted by ldsjohn View Post
    can I reuse q, like if I have three querys is there somethign i should do to to clear it, or can I just q.prepare again?
    AFAIK prepare() should be enough, but of course if you want to execute the same query several times, it will be enough if you prepare it once.

Similar Threads

  1. String value
    By Gayathri in forum Newbie
    Replies: 2
    Last Post: 21st November 2006, 05:44
  2. Create pixmap image from string
    By Morea in forum Qt Programming
    Replies: 5
    Last Post: 17th November 2006, 16:38
  3. inserting string > 127 characters to MS Access
    By jh in forum Qt Programming
    Replies: 0
    Last Post: 12th May 2006, 17:11
  4. How to get size (length, width ....) of string or char
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2006, 09:55
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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
  •  
Qt is a trademark of The Qt Company.