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] how to speed up. QSQLite INSERT query?
Qt 6.11 is out! See what's new in the release blog

[Solved] how to speed up. QSQLite INSERT query?

Scheduled Pinned Locked Moved General and Desktop
15 Posts 4 Posters 13.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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #2

    Do you use a QTableModel or a QSqlQuery?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #3

      O_O~~ hello. js_dev.
      we use. QSqlQuery.

      //code
      @
      QSqlQuery query(db);
      query.prepare("DROP TABLE ");
      query.exec()
      query.prepare("CREATE TABLE ");
      query.exec()
      query.prepare("INSERT INTO ");
      query.exec()
      @

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #4

        In my experience QSqlQuery is much faster than QTableModel. So that should be fine.
        But I gues without some code it is hard to say what the reason is.

        Have you started a transaction before inserting data?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #5

          i was test. two codes.
          but. same speed.;;;;;

          //not use prepare. addBindValue.
          @
          QString tmp, str;
          for(int row = 0; row < nRowCount; row++)
          {
          QString qsQuery = qsInsertTable;
          int nValueCount = 0;
          for(int col = 0; col < nColumnCount; col++)
          {
          qApp->processEvents();
          QModelIndex modelIndex = pModel->index(row, col);
          QVariant v = pModel->data(modelIndex);
          if(!v.isNull())
          {
          qsQuery += "'" + v.toString() + "',";
          nValueCount++;

                  //
                  tmp.sprintf("%d %d ", row, col);
                  str += tmp + v.toString() + "/ ";
              }
              else
              {
                  qsQuery += "'',";
              }
          }
          
          if(nValueCount == 0)
          {
              str += "\n";
              continue;
          }
          
          qsQuery.chop(1);
          qsQuery += ")";
          if (!query.exec&#40;qsQuery&#41;)
          {
              qCritical() << query.lastError();
          }
          str += "\n";
          

          }
          qDebug() << str;
          @

          //use prepare. addBindValue.
          @
          QString tmp, str;
          for(int row = 0; row < nRowCount; row++)
          {
          query.prepare(qsInsertTable);
          int nValueCount = 0;
          for(int col = 0; col < nColumnCount; col++)
          {
          qApp->processEvents();
          QModelIndex modelIndex = pModel->index(row, col);
          QVariant v = pModel->data(modelIndex);
          if(!v.isNull())
          {
          query.addBindValue(v.toString());
          nValueCount++;

                  //
                  tmp.sprintf("%d %d ", row, col);
                  str += tmp + v.toString() + "/ ";
              }
              else
              {
                  query.addBindValue(QString(""));
              }
          }
          
          if(nValueCount == 0)
          {
              str += "\n";
              continue;
          }
          
          if (!query.exec&#40;&#41;)
          {
              qCritical() << query.lastError();
          }
          str += "\n";
          

          }
          @

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mabrand
            wrote on last edited by
            #6

            You should probably check this "faq":http://www.sqlite.org/faq.html#q19.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #7

              thank you. mabrand.
              i will see.

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #8

                I do it like described "here":http://doc.qt.nokia.com/4.7/qsqlquery.html#prepare.

                This works fine and is fast enough for my software.

                Don't forget the @db.transaction()@ and @db.commit()@ before and after your query.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #9

                  O_T js_dev.
                  thank you for being so caring.

                  i just do it!

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #10

                    thank you. very much. js_dev.
                    and. with mabrand.

                    i success.
                    i applyed transaction() and commit().
                    and very fast write!.

                    have a nice day~~ O_O~~;;;;

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #11

                      Great :-)

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #12

                        O_O~~

                        1 Reply Last reply
                        0
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #13

                          [quote author="shint" date="1296545085"]
                          //use prepare. addBindValue.
                          @
                          QString tmp, str;
                          for(int row = 0; row < nRowCount; row++)
                          {
                          query.prepare(qsInsertTable);
                          int nValueCount = 0;
                          for(int col = 0; col < nColumnCount; col++)
                          {
                          @[/quote]

                          You should prepare your statement once (outside the for row statement) and use it multiple times. You actually do it the other way round: You create a single new prepared statement for each row you insert. This does not save you time, but cost you.

                          1 Reply Last reply
                          0
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by
                            #14

                            [quote author="Volker" date="1296556882"]
                            [quote author="shint" date="1296545085"]
                            //use prepare. addBindValue.
                            @
                            QString tmp, str;
                            for(int row = 0; row < nRowCount; row++)
                            {
                            query.prepare(qsInsertTable);
                            int nValueCount = 0;
                            for(int col = 0; col < nColumnCount; col++)
                            {
                            @[/quote]

                            You should prepare your statement once (outside the for row statement) and use it multiple times. You actually do it the other way round: You create a single new prepared statement for each row you insert. This does not save you time, but cost you.[/quote]

                            Good point! I haven't seen this.

                            1 Reply Last reply
                            0
                            • ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #15

                              O_O'''' oh! i understand.
                              Volker. good man. thank you.

                              to prepare() for a worry I will try to use.

                              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