What to pass to QSqlQuery::prepare() other than const char *
-
Hi :-)
I have
QT_NO_CAST_FROM_ASCII
set and use a lot of SQLite queries in my project. I wonder what's the best choice to pass the defined SQL queries toQSqlQuery::prepare()
(sorry if this is a dumb question, I read the docs but I'm not sure if I fully understand them).Should one use
const QString::fromUtf8("SOME QUERY")
,QLatin1String("SOME QUERY")
orQStringLiteral("SOME QUERY")
?Thanks for all clarification/explanation!
Cheers, Tobias
-
This applies to all methods taking a
QString
, not justQSqlQuery::prepare()
- Most of the cases:
QStringLiteral("foo")
if it will actually be converted toQString
QLatin1String("foo")
if it is use with a function that has an overload forQLatin1String
. (such asoperator==
,operator+
,startWith
,replace
, ...)
- Most of the cases:
-
@l3u_
No.
useQStringLiteral("foo")
to generate an immutableQString
, useQLatin1String
only in methods that explicitly have aQLatin1String
overloadNote that I said [immutable
QString
], not [immutable string]. For example, you still must pass a rawconst char*
toQObject::tr
-
So for the above case of
QSqlQuery::prepare(const QString &)
, theQStringLiteral
is the right choice, yes?Yes!
What @VRonin (and the docs) is saying is:
- If the signature is
const QString &
then useQStringLiteral
. - Only if the signature is
QLatin1String
then useQLatin1String
. (I imagine this will not be that many cases.)
- If the signature is