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

QMYSQL insert multiple rows

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 1.9k Views
  • 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 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
    
    
    jsulmJ 1 Reply Last reply
    0
    • R_IrudezuR Offline
      R_IrudezuR Offline
      R_Irudezu
      wrote on 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

        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
        
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on 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 SGaistS 2 Replies Last reply
        2
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #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
          • jsulmJ jsulm

            @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 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
            • jsulmJ jsulm

              @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?

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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
              0
              • SGaistS SGaist

                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 last edited by
                #7

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

                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