PDA

View Full Version : Insertion double quote into string/qstring



MarkoSan
14th December 2007, 07:05
Hello, I have this code:
// query string;
QString queryString("SELECT * FROM merchandize WHERE PicPath="%1";");
queryString.arg(strPicPath);

Once I try to compile it, I get following error:
CMerchandizeBrowser.cpp: In member function `QString CMerchandizeBrowser::getMerchandizeName(QString)':
CMerchandizeBrowser.cpp:330: error: invalid operands of types `const char[41]' and `int' to binary `operator%'

I need those quotes because SQL syntax. Please help!

jpn
14th December 2007, 07:26
See Escape character (http://en.wikipedia.org/wiki/Escape_character)

MarkoSan
14th December 2007, 08:06
Ok, but code:
Q_ASSERT(strPicPath.size()>0); // assertion test //
query string;
QString queryString("SELECT * FROM merchandize WHERE PicPath=\"%1\";");
QueryString.arg(strPicPath);
qDebug() << "queryString: " << queryString; // debugin the last line (qDebug) reports:
queryString: "SELECT * FROM merchandize WHERE PicPath="%1";"which is not ok, instead of '%1' there should be the contens of paramaterer strPicPath. I do not get it.

jpn
14th December 2007, 08:19
QString::arg() returns a copy of the string where the replacements have been done.

MarkoSan
14th December 2007, 08:24
So, it is ok then?

jpn
14th December 2007, 08:30
Are you using the return value of QString::arg()? It's an ordinary function which returns a value.

MarkoSan
14th December 2007, 09:04
Yep, I do. Let me give you complete code:
QString CMerchandizeBrowser::getMerchandizeName(QString strPicPath)
{
Q_ASSERT(strPicPath.size()>0); // assertion test
// query string;
QString queryString("SELECT * FROM merchandize WHERE PicPath=\"%1\";");
queryString.arg(strPicPath);
qDebug() << "queryString: " << queryString; // debug

QSqlQuery query; // sql query
query.exec(queryString); // executes query
Q_ASSERT(query.value(iMerchandizeFieldNAME).toStri ng().size()>0); // assertion test
qDebug() << query.value(iMerchandizeFieldNAME).toString(); // debug
return(query.value(iMerchandizeFieldNAME).toString ()); // returns merchandize name
}

jpn
14th December 2007, 09:06
Yep, I do. Let me give you complete code:

[...]
queryString.arg(strPicPath);
[...]


To me it looks you don't. Notice that QString::arg() is a const method. Again, it does not modify the original value but returns a modified value.

MarkoSan
14th December 2007, 09:11
I have this system everywhere in project and it works fine, just here it does not work. So, how should I rewrite the code, have you any clues?

jpn
14th December 2007, 09:24
I have this system everywhere in project and it works fine, just here it does not work. So, how should I rewrite the code, have you any clues?
Give yourself a moment to think it. Consider the following piece of pseudo code:


QString QString::arg(value) const
{
// does not modify _this_ but returns a copy
QString copy(*this);
copy.replacePlaceHolderWithValue(value);
return copy;
}


What's the content of foo?


QString foo("%1");
foo.arg(1234);


What's the content of bar?


QString bar("%1");
bar = bar.arg(5678);

MarkoSan
14th December 2007, 11:25
foo has 1234

and bar has 5678

Am I right?

jpn
14th December 2007, 11:56
foo has 1234

and bar has 5678

Am I right?
Unfortunately not (foo has "%1" and bar has "5678"). However, did you notice the difference? Looks subtle, but is in fact significant.

MarkoSan
14th December 2007, 12:23
Aha, I must use = operator to set right value. God damn, thanks. :confused: