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. [SOLVED] Exec full script by QSqlDatabase
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Exec full script by QSqlDatabase

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 8.3k 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.
  • Z Offline
    Z Offline
    ZapB
    wrote on last edited by
    #2

    The QSqlDataBase::exec() function is just a convenience function to save you having to create a QSqlQuery object yourself. Reading the docs you'll see that it executes a single SQL statement not an entire sql script.

    You are responsible for passing the script statements to the QSqlDataBase yourself either using exec() or by creating QSqlQuery objects. Don't forget to check that each was successful before moving onto the next.

    Nokia Certified Qt Specialist
    Interested in hearing about Qt related work

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maxim.prishchepa
      wrote on last edited by
      #3

      Tnx, i solve this problem that:
      @QSqlQuery query(m_db);
      QStringList queryes = script.split(QChar(';'));
      foreach(QString queryString, queryes){
      query.exec(queryString);
      }
      @

      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZapB
        wrote on last edited by
        #4

        Don't forget to check for any errors after issuing each query.

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #5

          [quote author="Maxim Prishchepa" date="1309855003"]Tnx, i solve this problem that:
          @QSqlQuery query(m_db);
          QStringList queryes = script.split(QChar(';'));
          foreach(QString queryString, queryes){
          query.exec(queryString);
          }
          @[/quote]

          This only works if you never have a semicolon (';') in your data.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #6

            Indeed. For safety, you need to do some more intelligent parsing for your split than just splitting on a semicolon.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maxim.prishchepa
              wrote on last edited by
              #7

              Tnx a lot!
              I'm rewrite code to this:
              @QString queryToExec;
              foreach(QString queryString, queryes){
              queryToExec += queryString;
              if(queryString.endsWith(QChar(')')) == false){
              queryToExec += ";";
              continue;
              }
              query.exec(queryToExec);
              queryToExec = QString();
              }@
              I think in current data field never happen a string like ");"

              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #8

                @
                CREATE sequence foo;
                CREATE sequence bar;
                @

                bang

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  maxim.prishchepa
                  wrote on last edited by
                  #9

                  Ah... :)
                  Why Qt dose not contains a SQL parser? :))

                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz).

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #10

                    [quote author="Maxim Prishchepa" date="1309862429"]Ah... :)
                    Why Qt dose not contains a SQL parser? :))[/quote]

                    Why should it? It's hard to implement and used only for corner cases... (yes I saw the smiley :-) )

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #11

                      Note that for those corner cases, it may be acceptable to access the API of the underlying driver directly. There may be support for sending multiple statements in one go at that level. Otherwise, perhaps you can look into using Predicate. I believe that that SQL framework does include an SQL parser.

                      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