Results 1 to 9 of 9

Thread: escape input for mysql server

  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 escape input for mysql server

    in Qt is there any equivalent function similar to "mysql_escape_string()" in PHP?

    what I am trying to accomplish is to allow a user to enter information into a QTextedit and then transfer the data into a blob in mysql. My code so far works perfectly to get the text edit info into the blob except for cases where certain special characters are entered. For instance entering:

    somestuff","somemorestuff
    into the text edit causes an error as my query interprets this as part of the SQL statement rather than something the user typed that should be put in the blob.

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: escape input for mysql server

    Quote Originally Posted by tpf80 View Post
    in Qt is there any equivalent function similar to "mysql_escape_string()" in PHP?
    what I am trying to accomplish is to allow a user to enter information into a QTextedit and then transfer the data into a blob in mysql. My code so far works perfectly to get the text edit info into the blob except for cases where certain special characters are entered. For instance entering:
    into the text edit causes an error as my query interprets this as part of the SQL statement rather than something the user typed that should be put in the blob.
    PHP having 5000 or more function. C++ is different...


    For any projekt i make a base class to extendend on evry place i need....

    I work so many on XML utf-8 and i put on sql as encodeBase64 and must not quote...
    php can read on http://php.net/base64-decode.php and the Dom XML friends....
    As caching Dom ist faster.... only must transform xslt ...the data pdf, word, openoffice, svg, csv, ecc...


    Qt Code:
    1. /* insert quote */
    2. QString sql_Quote( QString xml )
    3. {
    4. QString text = xml;
    5. text.replace("\"","\\\""); /* 228 ``*/
    6. text.replace("'","`");
    7. return text;
    8. }
    9.  
    10. /* select quote */
    11. QString sql_DeQuote( QString xml )
    12. {
    13. QString text = xml;
    14. text.replace("\\\"","\""); /* 228 ``*/
    15. text.replace("`","\'");
    16. return text;
    17. }
    18.  
    19.  
    20. /* encode to name */
    21. QString encodeBase64( QString xml )
    22. {
    23. QByteArray text;
    24. text.append(xml);
    25. return text.toBase64();
    26. }
    27.  
    28. /* decode to name */
    29. QString decodeBase64( QString xml )
    30. {
    31. QByteArray xcode("");;
    32. xcode.append(xml);
    33. QByteArray precode(QByteArray::fromBase64(xcode));
    34. QString notetxt = precode.data();
    35. return notetxt;
    36. }
    To copy to clipboard, switch view to plain text mode 

    is your QTextedit data xhtml? image and so.....


    IMO: QTextedit xhtml and image or attachment i zip the file to mysql

    blob (mysql longblob 2GB max 4MB query setting) data zip file

    QByteArray zipstream;
    zipstream.toBase64();
    and after to replace field.... is so clean and small data...

    SAMPLE: my new Home-Page http://www.swisse.net/ work only on 2 xslt file
    http://ciz.ch/map/theme/zest/page.xsl
    http://ciz.ch/map/theme/zest/master_html.xsl
    PHP unzip data as cache folder , like this http://www.swisse.net/data/2007/01/01/50000/ and xslt transform all no sql query... only admin running sql.

  3. #3
    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: escape input for mysql server

    Try preparing the query and then binding values (QSqlQuery::prepare() and QSqlQuery::bindValue()), they should escape the data for you.

  4. The following user says thank you to wysota for this useful post:

    tpf80 (16th June 2007)

  5. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: escape input for mysql server

    Quote Originally Posted by wysota View Post
    Try preparing the query and then binding values (QSqlQuery::prepare() and QSqlQuery::bindValue()), they should escape the data for you.
    Yes work fine on sqlite or other db type != not on mysql Collation special like CHARSET=utf-8 or other koi8r .... qt take from CHARSET QLocale and pusch on DBs and not read "SHOW COLUMNS FROM TABLE" to become the correct CHARSET, if table having iso latin 1 an OS QLocale is the same works....

  6. The following user says thank you to patrik08 for this useful post:

    tpf80 (16th June 2007)

  7. #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: escape input for mysql server

    thanks for your help, "bindValue()" is the way to go in this instance. am now able to add records with non-standard characters in them and it works fine.

  8. #6
    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: escape input for mysql server

    I also noticed that I can use "bindValue()" for select statements, even though the examples only show them for inserts:

    Qt Code:
    1. query.prepare("select userid, username, firstname, lastname, email, status from user where username=:username");
    2. query.bindValue(":username", login.username);
    3. if (!query.exec()) {
    4. //do something to handle the error here
    5. } else {
    6. while (query.next()) {
    7. //do something with the results we got here
    8. }
    9. }
    10. ]
    To copy to clipboard, switch view to plain text mode 

    is this a proper use of "bindValue()"?

  9. #7
    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: escape input for mysql server

    Quote Originally Posted by patrik08 View Post
    Yes work fine on sqlite or other db type != not on mysql Collation special like CHARSET=utf-8 or other koi8r .... qt take from CHARSET QLocale and pusch on DBs and not read "SHOW COLUMNS FROM TABLE" to become the correct CHARSET, if table having iso latin 1 an OS QLocale is the same works....
    If you have the database set incorrectly then don't expect everything to work.


    Quote Originally Posted by tpf80 View Post
    I also noticed that I can use "bindValue()" for select statements, even though the examples only show them for inserts:

    (...)

    is this a proper use of "bindValue()"?
    Sure

  10. The following user says thank you to wysota for this useful post:

    tpf80 (17th June 2007)

  11. #8
    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: escape input for mysql server

    One last question I notice that:
    Qt Code:
    1. #
    2. query.prepare("select userid, username, firstname, lastname, email, status from user where username=:username");
    3. #
    4. query.bindValue(":username", login.username);
    To copy to clipboard, switch view to plain text mode 
    Pretty much escapes the login.username, puts it in quotes, and inserts it where :username is. However how do I use bindValue() with queries using "like" for example:

    Qt Code:
    1. query.prepare("select userid, username, firstname, lastname, email, status from user where username like \"%" + something + "%\"");
    To copy to clipboard, switch view to plain text mode 

    it doesn't seem that
    Qt Code:
    1. query.prepare("select userid, username, firstname, lastname, email, status from user where username like \"%:something%\"");
    2. query.bindValue(":something", somevariable);
    To copy to clipboard, switch view to plain text mode 
    works properly. (gives an error preparing the query). What is the proper way to use bindvalue with a "like" query?

  12. #9
    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: escape input for mysql server

    Quote Originally Posted by tpf80 View Post
    What is the proper way to use bindvalue with a "like" query?
    Try:
    SQL Code:
    1. SELECT userid, username, firstname, lastname, email, STATUS
    2. FROM user
    3. WHERE username LIKE '%' || :something || '%'
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. query.bindValue(":something", "%" + somevariable + "%" );
    To copy to clipboard, switch view to plain text mode 

  13. The following user says thank you to jacek for this useful post:

    tpf80 (18th June 2007)

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.