for循环批处理数据并将其丢弃在c ++中

时间:2014-10-01 07:27:16

标签: c++ mysql

void write_token_to_data()
    {
        typedef double* DynamicMatrix[l+m];
        // DynamicMatrix Count;
        typedef double* DynamicMatrix2[l+m];
        //DynamicMatrix2 Prob;
        for(int i=0; i<(l+m); i++)
        {
            for(int j=0; j<(l+m); j++)
            {
                if(mysqlinsert2(i,j,combine[i],combine[j]))
                {
                    cout<<"insert OK!!"<<endl;
                }
                else
                {
                    cout<<"insert failed"<<endl;
                }


            }
        }

    }//end of function

这是我的问题 我怎么能改变这个子功能,我可以保持i,j结合[i],结合[j] 直到数字100,并将这四个值抛入mysqlinsert函数 并在for循环索引101中释放数组并继续记录值,长话短说,我想把这个mysql插入到小部分中

1 个答案:

答案 0 :(得分:1)

 - You can contruct a string in the form
(i,j,cobine[i],combine[j]) for each iteration.
 - Inorder to insert a multiple rows in a single query.
[Normal insert query for multiple records 
 INSERT INTO Table ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )
]
 - also rewrite mysqlinsert2(i,j,combine[i],combine[j]) into *mysqlinsert2(str)*.and execute the string.

    for(int i=0; i<(l+m); i++){
      for(int j=0; j<(l+m); j++){
        if (till count => 100) {
          // concatinate the string with prev str
          str += (i,j,combine[i],combine[j])+',';
        }
        if (count reaches hundred || count = 0) {
          // execute the string 
          mysqlinsert2(str);
          // initialize str as empty string
          str = 'INSERT INTO Table ( Column1, Column2, column3, column4 ) VALUES';
          count = 0;
        }
      }
    }