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 ?
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 ?
Why? But if you really have to, use QSqlField for escaping.
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!
Well, it could, butfor that QSqlQuery::prepare and QSqlQuery::bindValue is exactly what you need.i need something like :
qstring = "O'neill";
qstring = mysql_escape_string( qstring ); // qstring now O\'neill
...
query.exec("Insert Into table (col) Values ( qstring ) ");
Although we are not in the newbie section:Qt Code:
db.setDatabaseName(":memory:"); db.open(); QSqlField f; f.setValue(stringToEscape); qWarning() << stringToEscape; qWarning() << db.driver()->formatValue(f);To copy to clipboard, switch view to plain text modeBut don't dare, don't even think of using that for your problem!"foo " bar"
"'foo " bar'"
ok, i tried your solution and appears to work, but why do you say it shouldnt be used ? what is problem ?
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.
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.
Bookmarks