What to pass to QSqlQuery::prepare() other than const char *
-
Hi :-)
I have
QT_NO_CAST_FROM_ASCIIset 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:
-
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:
-
@VRonin So … one should generally use
QLatin1Stringwherever one defines an immutable string?@l3u_
No.
useQStringLiteral("foo")to generate an immutableQString, useQLatin1Stringonly in methods that explicitly have aQLatin1StringoverloadNote that I said [immutable
QString], not [immutable string]. For example, you still must pass a rawconst char*toQObject::tr -
@l3u_
No.
useQStringLiteral("foo")to generate an immutableQString, useQLatin1Stringonly in methods that explicitly have aQLatin1StringoverloadNote that I said [immutable
QString], not [immutable string]. For example, you still must pass a rawconst char*toQObject::tr -
@VRonin Okay. So for the above case of
QSqlQuery::prepare(const QString &), theQStringLiteralis the right choice, yes? I also read (at least a part of) that blog post you cited, but I still didn't know when to use what ;-)So for the above case of
QSqlQuery::prepare(const QString &), theQStringLiteralis 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
QLatin1Stringthen useQLatin1String. (I imagine this will not be that many cases.)
- If the signature is
-
So for the above case of
QSqlQuery::prepare(const QString &), theQStringLiteralis 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
QLatin1Stringthen useQLatin1String. (I imagine this will not be that many cases.)
- If the signature is