Insertion double quote into string/qstring
Hello, I have this code:
Code:
// query string;
QString queryString
("SELECT * FROM merchandize WHERE PicPath="%1
";");
queryString.arg(strPicPath);
Once I try to compile it, I get following error:
Code:
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!
Re: Insertion double quote into string/qstring
Re: Insertion double quote into string/qstring
Ok, but code:
Code:
Q_ASSERT(strPicPath.size()>0); // assertion test //
query string;
QString queryString
("SELECT * FROM merchandize WHERE PicPath=\"%1\";");
QueryString.arg(strPicPath);
qDebug() << "queryString: " << queryString; // debug
in the last line (qDebug) reports:
Code:
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.
Re: Insertion double quote into string/qstring
QString::arg() returns a copy of the string where the replacements have been done.
Re: Insertion double quote into string/qstring
Re: Insertion double quote into string/qstring
Are you using the return value of QString::arg()? It's an ordinary function which returns a value.
Re: Insertion double quote into string/qstring
Yep, I do. Let me give you complete code:
Code:
{
Q_ASSERT(strPicPath.size()>0); // assertion test
// query string;
QString queryString
("SELECT * FROM merchandize WHERE PicPath=\"%1\";");
queryString.arg(strPicPath);
qDebug() << "queryString: " << queryString; // debug
query.exec(queryString); // executes query
Q_ASSERT(query.value(iMerchandizeFieldNAME).toString().size()>0); // assertion test
qDebug() << query.value(iMerchandizeFieldNAME).toString(); // debug
return(query.value(iMerchandizeFieldNAME).toString()); // returns merchandize name
}
Re: Insertion double quote into string/qstring
Quote:
Originally Posted by
MarkoSan
Yep, I do. Let me give you complete code:
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.
Re: Insertion double quote into string/qstring
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?
Re: Insertion double quote into string/qstring
Quote:
Originally Posted by
MarkoSan
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:
Code:
{
// does not modify _this_ but returns a copy
copy.replacePlaceHolderWithValue(value);
return copy;
}
What's the content of foo?
What's the content of bar?
Code:
bar = bar.arg(5678);
Re: Insertion double quote into string/qstring
foo has 1234
and bar has 5678
Am I right?
Re: Insertion double quote into string/qstring
Quote:
Originally Posted by
MarkoSan
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.
Re: Insertion double quote into string/qstring
Aha, I must use = operator to set right value. God damn, thanks. :confused: