Results 1 to 8 of 8

Thread: Escape string for insertion into Mysql db ??

  1. #1
    Join Date
    Feb 2010
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Escape string for insertion into Mysql db ??

    I know that q.bindValue(laceholder, stringvar) will escape stringvar, but what I need is like escaped_str = mysql_real_escape_string(string) so that I can escape myself.


    Any hints as to how to approach this problem ?

  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: Escape string for insertion into Mysql db ??

    Quote Originally Posted by BillGates View Post
    but what I need is like escaped_str = mysql_real_escape_string(string) so that I can escape myself.
    Why? But if you really have to, use QSqlField for escaping.

  3. #3
    Join Date
    Feb 2010
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Escape string for insertion into Mysql db ??

    erm, thanks for replying, I was not aware of the existance of QSqlField, but i dont think it can help me.

    i need something like :

    qstring = "O'neill";
    qstring = mysql_escape_string( qstring ); // qstring now O\'neill
    ...
    query.exec("Insert Into table (col) Values ( qstring ) ");

    Cheers!

  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: Escape string for insertion into Mysql db ??

    Quote Originally Posted by BillGates View Post
    erm, thanks for replying, I was not aware of the existance of QSqlField, but i dont think it can help me.
    Well, it could, but
    i need something like :

    qstring = "O'neill";
    qstring = mysql_escape_string( qstring ); // qstring now O\'neill
    ...
    query.exec("Insert Into table (col) Values ( qstring ) ");
    for that QSqlQuery::prepare and QSqlQuery::bindValue is exactly what you need.

  5. #5
    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: Escape string for insertion into Mysql db ??

    Although we are not in the newbie section:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(":memory:");
    3. db.open();
    4.  
    5. QString stringToEscape = "foo \" bar";
    6. f.setType(QVariant::String);
    7. f.setValue(stringToEscape);
    8.  
    9. qWarning() << stringToEscape;
    10. qWarning() << db.driver()->formatValue(f);
    To copy to clipboard, switch view to plain text mode 
    "foo " bar"
    "'foo " bar'"
    But don't dare, don't even think of using that for your problem!

  6. #6
    Join Date
    Feb 2010
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Escape string for insertion into Mysql db ??

    ok, i tried your solution and appears to work, but why do you say it shouldnt be used ? what is problem ?

  7. #7
    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: Escape string for insertion into Mysql db ??

    Because it is nonsense to do so if you have a prepare function. Even if I haven't checked, the prepare function is probably faster then doing the encoding yourself.

    And I always would stick the the functions Qt provides you and I wouldn't do voodoo like going with QSqlField and QSqlDriver if there is no strong reason.

  8. #8
    Join Date
    Feb 2010
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Escape string for insertion into Mysql db ??

    OK, but what if what you want is just to write down (to a file) the query for later insertion ... you need to escape the string values.

Similar Threads

  1. Replies: 2
    Last Post: 10th December 2009, 20:51
  2. qDebug style string insertion
    By rbp in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2009, 03:24
  3. Get Korean string from MYSQL(KUBUNTU)
    By morgana in forum Newbie
    Replies: 2
    Last Post: 7th July 2008, 07:57
  4. Insertion double quote into string/qstring
    By MarkoSan in forum General Programming
    Replies: 12
    Last Post: 14th December 2007, 12:23
  5. escape input for mysql server
    By tpf80 in forum Qt Programming
    Replies: 8
    Last Post: 18th June 2007, 22:58

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