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. QMYSQL insert multiple rows
Forum Updated to NodeBB v4.3 + New Features

QMYSQL insert multiple rows

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 1.9k Views 2 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.
  • D Offline
    D Offline
    Dydy02
    wrote on 11 Nov 2018, 23:28 last edited by Dydy02
    #1

    i try to insert multiple rows in my database wich data from an array(QStrinList) so i did it like this. and it may have an error. the query excuted but the program crash after execution with this error: The program has unexpectedly finished.

    //your code here
    void Thor::full_database()
    {
       QString namelist;
         QFile file("C:/Users/Wendy/Desktop/Zone_unops.txt");
    
        if(!file.open(QFile::ReadOnly|QFile::Text))
             {
          qDebug()<<"file couldnt not open";
    
            }
        else
        {
         qDebug()<<"file opened";
    
              QTextStream in(&file);
               namelist= in.readAll();
              QStringList listname = namelist.split(",",QString::SkipEmptyParts);
              listname.sort();
              file.close();
    int i=1;
              open_data();
              QSqlQuery query;
             while(i<=listname.size())
            {
           query.prepare("insert into zone (Name) values(:allname)");
          query.bindValue(":allname",listname[i]);
                      if(!query.exec())
                      {
                  i=listname.size()+1; // for cancelling the loop
                  QMessageBox::critical(this,tr("error::"),query.lastError().text());
                      } // ending if !query.exec
    
                      else
                      {            
                      i++;
                      }// ending else query.exec
    
            }//ending while
    
    close_data();
    
    } // ending else if file open
    
    } // ending function full_database
    
    
    J 1 Reply Last reply 12 Nov 2018, 05:38
    0
    • R Offline
      R Offline
      R_Irudezu
      wrote on 12 Nov 2018, 01:52 last edited by
      #2

      Please update your topic, specify your title and explain your problem in the paragraph textbox, not in the title. Then wait for a reply who can help you :)

      Keizoku wa chikaranari.

      1 Reply Last reply
      5
      • D Dydy02
        11 Nov 2018, 23:28

        i try to insert multiple rows in my database wich data from an array(QStrinList) so i did it like this. and it may have an error. the query excuted but the program crash after execution with this error: The program has unexpectedly finished.

        //your code here
        void Thor::full_database()
        {
           QString namelist;
             QFile file("C:/Users/Wendy/Desktop/Zone_unops.txt");
        
            if(!file.open(QFile::ReadOnly|QFile::Text))
                 {
              qDebug()<<"file couldnt not open";
        
                }
            else
            {
             qDebug()<<"file opened";
        
                  QTextStream in(&file);
                   namelist= in.readAll();
                  QStringList listname = namelist.split(",",QString::SkipEmptyParts);
                  listname.sort();
                  file.close();
        int i=1;
                  open_data();
                  QSqlQuery query;
                 while(i<=listname.size())
                {
               query.prepare("insert into zone (Name) values(:allname)");
              query.bindValue(":allname",listname[i]);
                          if(!query.exec())
                          {
                      i=listname.size()+1; // for cancelling the loop
                      QMessageBox::critical(this,tr("error::"),query.lastError().text());
                          } // ending if !query.exec
        
                          else
                          {            
                          i++;
                          }// ending else query.exec
        
                }//ending while
        
        close_data();
        
        } // ending else if file open
        
        } // ending function full_database
        
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 12 Nov 2018, 05:38 last edited by
        #3

        @Dydy02 said in i try to insert multiple rows in my database but data come from an array(QStrinList) so i did it like this. and it may have and error. the query excuted but the program crash after execution with this error: The program has unexpectedly finished.:

        while(i<=listname.size())

        Should be

        while(i<listname.size())
        

        Also, why do you use a while loop instead of a for loop?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        D S 2 Replies Last reply 15 Nov 2018, 15:39
        2
        • V Offline
          V Offline
          VRonin
          wrote on 12 Nov 2018, 08:27 last edited by VRonin 11 Dec 2018, 09:34
          #4

          If your database supports it, a batch insertion is probably what you want. see http://doc.qt.io/qt-5/qsqlquery.html#execBatch

          P.S.
          All containers in C++ start their indexes at 0 and end at size()-1

          "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

          1 Reply Last reply
          2
          • J jsulm
            12 Nov 2018, 05:38

            @Dydy02 said in i try to insert multiple rows in my database but data come from an array(QStrinList) so i did it like this. and it may have and error. the query excuted but the program crash after execution with this error: The program has unexpectedly finished.:

            while(i<=listname.size())

            Should be

            while(i<listname.size())
            

            Also, why do you use a while loop instead of a for loop?

            D Offline
            D Offline
            Dydy02
            wrote on 15 Nov 2018, 15:39 last edited by Dydy02
            #5

            @jsulm thank you. i'm so nervous i know that array start at array[0] but i did it. so ridiculous. i use a while loop instead of a for loop so i can stop it in case.
            it only missed -1 in the loop

            //your code here
             while(i<=listname.size()-1)
            
            1 Reply Last reply
            0
            • J jsulm
              12 Nov 2018, 05:38

              @Dydy02 said in i try to insert multiple rows in my database but data come from an array(QStrinList) so i did it like this. and it may have and error. the query excuted but the program crash after execution with this error: The program has unexpectedly finished.:

              while(i<=listname.size())

              Should be

              while(i<listname.size())
              

              Also, why do you use a while loop instead of a for loop?

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 15 Nov 2018, 19:25 last edited by
              #6

              Hi

              You should rather follow @jsulm's suggestion:
              @jsulm said in QMYSQL insert multiple rows:

              Should be

              while(i<listname.size())
              

              It's simpler and less error prone.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              D 1 Reply Last reply 17 Nov 2018, 21:53
              0
              • S SGaist
                15 Nov 2018, 19:25

                Hi

                You should rather follow @jsulm's suggestion:
                @jsulm said in QMYSQL insert multiple rows:

                Should be

                while(i<listname.size())
                

                It's simpler and less error prone.

                D Offline
                D Offline
                Dydy02
                wrote on 17 Nov 2018, 21:53 last edited by
                #7

                @SGaist thank you . it is better. less code.

                1 Reply Last reply
                0

                6/7

                15 Nov 2018, 19:25

                • Login

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