Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What to pass to QSqlQuery::prepare() other than const char *
Forum Updated to NodeBB v4.3 + New Features

What to pass to QSqlQuery::prepare() other than const char *

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • l3u_L Offline
    l3u_L Offline
    l3u_
    wrote on last edited by
    #1

    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 to QSqlQuery::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") or QStringLiteral("SOME QUERY")?

    Thanks for all clarification/explanation!

    Cheers, Tobias

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      This applies to all methods taking a QString, not just QSqlQuery::prepare()

      • Most of the cases: QStringLiteral("foo") if it will actually be converted to QString
      • QLatin1String("foo") if it is use with a function that has an overload for QLatin1String. (such as operator==, operator+, startWith, replace, ...)

      Source https://woboq.com/blog/qstringliteral.html

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      l3u_L 1 Reply Last reply
      2
      • VRoninV VRonin

        This applies to all methods taking a QString, not just QSqlQuery::prepare()

        • Most of the cases: QStringLiteral("foo") if it will actually be converted to QString
        • QLatin1String("foo") if it is use with a function that has an overload for QLatin1String. (such as operator==, operator+, startWith, replace, ...)

        Source https://woboq.com/blog/qstringliteral.html

        l3u_L Offline
        l3u_L Offline
        l3u_
        wrote on last edited by
        #3

        @VRonin So … one should generally use QLatin1String wherever one defines an immutable string?

        VRoninV 1 Reply Last reply
        0
        • l3u_L l3u_

          @VRonin So … one should generally use QLatin1String wherever one defines an immutable string?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @l3u_
          No.
          use QStringLiteral("foo") to generate an immutable QString, use QLatin1String only in methods that explicitly have a QLatin1String overload

          Note that I said [immutable QString], not [immutable string]. For example, you still must pass a raw const char* to QObject::tr

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          l3u_L 1 Reply Last reply
          2
          • VRoninV VRonin

            @l3u_
            No.
            use QStringLiteral("foo") to generate an immutable QString, use QLatin1String only in methods that explicitly have a QLatin1String overload

            Note that I said [immutable QString], not [immutable string]. For example, you still must pass a raw const char* to QObject::tr

            l3u_L Offline
            l3u_L Offline
            l3u_
            wrote on last edited by l3u_
            #5

            @VRonin Okay. So for the above case of QSqlQuery::prepare(const QString &), the QStringLiteral is 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 ;-)

            JonBJ 1 Reply Last reply
            0
            • l3u_L l3u_

              @VRonin Okay. So for the above case of QSqlQuery::prepare(const QString &), the QStringLiteral is 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 ;-)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @l3u_

              So for the above case of QSqlQuery::prepare(const QString &), the QStringLiteral is the right choice, yes?

              Yes!

              What @VRonin (and the docs) is saying is:

              • If the signature is const QString & then use QStringLiteral.
              • Only if the signature is QLatin1String then use QLatin1String . (I imagine this will not be that many cases.)
              l3u_L 1 Reply Last reply
              1
              • JonBJ JonB

                @l3u_

                So for the above case of QSqlQuery::prepare(const QString &), the QStringLiteral is the right choice, yes?

                Yes!

                What @VRonin (and the docs) is saying is:

                • If the signature is const QString & then use QStringLiteral.
                • Only if the signature is QLatin1String then use QLatin1String . (I imagine this will not be that many cases.)
                l3u_L Offline
                l3u_L Offline
                l3u_
                wrote on last edited by
                #7

                @JonB Thanks for the clarification. The docs are somewhat confusing ("It's better, but beware, it's not better for all cases") …

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved