2016-05-25 9 views
0

私は、C++を使ってmysqlアプリケーションを作っています。
私は必要な行を取得するコードを作成したいと思います。
次に、私はこの行の値を取る。
私は最初の配列に配置し、この行が終了するまで列を反復処理します。
次に、次の行をarray2に入れます。
次に、array1の全体をarray型のベクトルに入れます。
私は次の行をループして同じ2つの配列に新しい行の値を入れますが、古い選択された行をベクトルで時間の経過とともに増加させる必要があるので、これを行います。
私は多くの方法を試してみましたが、私はベクトルのポインタは配列の実際のデータではないと思います。
私は1
最新のを見つける古いベクトル値を取得しようとすると、これはコードです:短期でmysql row [x]から構築された固定長配列を含むベクトル

if(mysql_query(conn, "SELECT * FROM price_ttl limit 10")) 
    {   
     print_error (conn,"");  
    } 
res = mysql_store_result(conn); 

//this variables are used to store information from res meta data using these functions 
totalrows = mysql_num_rows(res); 
numfields = mysql_num_fields(res); 
/*here we will take value of each column in each row as string 
then we will put it in certain position in array of 2 arrays 
this array reperesnt 1 row 
each array is formed of "12-1"numfields-1 places */    
const int maxrows_price_ttl = 2;  
const int maxcolumns_price_ttl = 11;//the last cloumn for null value 
char two_rows_values_eleven_fields_1strow[maxcolumns_price_ttl]; 
char two_rows_values_eleven_fields_2ndrow[maxcolumns_price_ttl]; 
//second we make the vector of multiple arrays for price_mtl table rows 
//int maxrows_price_mtl_var ; 
//const int maxcolumns_price_mtl = 11; 
//we define vector "dynamic array" which is of the type array and call it variable_rows_values_eleven_fields_vector 
//the array which represent the type of the vector is called single_row_values_eleven_fields_array 
//char *single_row_values_eleven_fields_array; 
//here we define the vector its type char* its name variable_rows_values_eleven_fields_vector 
std::vector<std::string> variable_rows_values_eleven_fields_vector_string; 
std::vector<char*>variable_rows_values_eleven_fields_vector_array; 

//this is to make sure we are present in place where mysql_fetch_row will get the desired row "row1" 
mysql_data_seek(res, 0); 
int rownumber=1; 
int columnnumber=0; 
std::string row_string=""; 
//char *row_array[];  
//now we want to loop through the result set 
for (rownumberall=1; rownumberall < totalrows; rownumberall++) 
    { 
     row_string=""; 
     //third we get the value of the current row from result set of current query 
     //row = mysql_fetch_row (res);//>>>>>>>>>>>>--1-->>>>>--3-- 
     //std::cout <<*(&row)<<std::endl;   
     //fputc ('\n', stdout); 
     //printf("Press any key to continue . . . "); 
     //fputc ('\n', stdout); 
     //_getch(); 
     for (rownumber=1; rownumber <= maxrows_price_ttl; rownumber++) 
      { 
       //third we get the value of the current row from result set of current query 
       row = mysql_fetch_row (res);//>>>>>>>>>>>>--1-->>>>>--3-- 

       for (columnnumber=0; columnnumber < numfields; columnnumber++) 
        { 
         if (rownumber==2) 
          { 
           //here we put each coloumn value in its place in array of arrays"which is similar to structure of price table from which we r geting our rows" 
           //we use rownumber-1 because array 1st place is zero first row is 1 first column is 0 
           two_rows_values_eleven_fields_2ndrow[columnnumber]= *row[columnnumber] ; //array first elemnt is zero >>>>>>>>>>>>>>--2--a 
           //std::cout <<two_rows_values_eleven_fields[rownumber-1][columnnumber]<<std::endl;       
           //printf("Press any key to continue . hawww. . "); 
           //fputc ('\n', stdout); 
           //_getch(); 
          } 
         //row_array[columnnumber]= row[columnnumber] ; 
         if (rownumber==1) 
          { 
           two_rows_values_eleven_fields_1strow[columnnumber]= *row[columnnumber] ; 
           std::cout <<row_string<<std::endl; 
           row_string=row_string+row[columnnumber]+"," ; 
          } 
         //row_string=row_string+two_rows_values_eleven_fields[0][columnnumber]+"," ; 

         //std::cout <<row_string<<std::endl;    
         //fputc ('\n', stdout); 
         //printf("Press any key to continue . . . "); 
         //fputc ('\n', stdout); 
         //_getch(); 

        } 


       if (rownumber==1)//so that we enter single time in vector 
        { 
         row_string.erase(row_string.end()-1); 
         row_string="'"+row_string+"'" ; 

         variable_rows_values_eleven_fields_vector_string.push_back(row_string);//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>--2--b 
         variable_rows_values_eleven_fields_vector_array.push_back(two_rows_values_eleven_fields_1strow);//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>--2--b 

        // std::cout <<row_string<<std::endl; 
        // std::cout <<variable_rows_values_eleven_fields_vector_string.back()<<std::endl; 
        // std::cout <<variable_rows_values_eleven_fields_vector_array.back()<<std::endl; 
        // std::cout <<variable_rows_values_eleven_fields_vector_array.back()[0]<<std::endl; 
        // std::cout <<variable_rows_values_eleven_fields_vector_array.back()[1]<<std::endl; 
        // std::cout <<variable_rows_values_eleven_fields_vector_array.back()[2]<<std::endl; 
        // fputc ('\n', stdout); 
        // printf("Press any key to continue . . . "); 
        // fputc ('\n', stdout); 
        // _getch(); 
        }       
      } 
      //compare their difference for having same sign 
      //first we need to convert from string to double 
      //first we make varibles 
      double price_ttl_pricediff_row_1_double=0; 
      double price_ttl_pricediff_row_2_double=0; 
      //here we make string variable for string value of price_ttl pricediff which is extruded from row1 column 6 row 2 column6 
      std::string str_price_ttl_pricediff_row_1=""; 
      std::string str_price_ttl_pricediff_row_2=""; 
      //this is char pointer required by function strtod to point to begining of string after end of double part of the string to be converted 
      char *strtodpend; 
      const char *str_to_char_pointer2;  
      //here we give variables of price_ttl pricediff its values 
      str_to_char_pointer2=&two_rows_values_eleven_fields_1strow[5]; 
      price_ttl_pricediff_row_1_double = strtod(str_to_char_pointer2,&strtodpend); 
      str_to_char_pointer2=&two_rows_values_eleven_fields_2ndrow[5];    
      price_ttl_pricediff_row_2_double = strtod(str_to_char_pointer2,&strtodpend); 
      //here this is how to get the sign of each of the two values 
      //sign(price_ttl_pricediff_row_1_double); 
      //sign(price_ttl_pricediff_row_2_double); 
      //this is to show the values of pricediff of price_ttl for test 
      std::cout <<two_rows_values_eleven_fields_1strow[5]<<std::endl; 
      std::cout <<two_rows_values_eleven_fields_2ndrow[5]<<std::endl;    
      std::cout <<str_to_char_pointer2 <<std::endl;   
      std::cout <<price_ttl_pricediff_row_2_double <<std::endl;   

      std::cout <<sign(price_ttl_pricediff_row_1_double)<<std::endl; 
      std::cout <<sign(price_ttl_pricediff_row_2_double) <<std::endl;    
      std::cout <<price_ttl_pricediff_row_1_double <<std::endl;   
      std::cout <<price_ttl_pricediff_row_2_double <<std::endl;   
      //std::cout <<str_price_ttl_pricediff <<std::endl;   
      //std::cout <<str_price_ttl_timediff <<std::endl;   
      //std::cout <<insert_into <<std::endl;   
      //fputc ('\n', stdout); 
      printf("Press any key to continue . . . "); 
      fputc ('\n', stdout); 
      _getch(); 
      //if signs are not the same 
      if (sign(price_ttl_pricediff_row_1_double)!=sign(price_ttl_pricediff_row_2_double)) 
       { 
        printf("Press any key to continue . . . "); 
        fputc ('\n', stdout); 
        _getch(); 

        //here we calculate values of the row of mtl table from contents of vector 
        //first we know contents of vector 
        int vector_size =variable_rows_values_eleven_fields_vector_array.size();//size represents number of rows of price_ttl which will form one row for price_mtl 
        //we get all information from first and last element in vector 
        char *first_vector_array_row=variable_rows_values_eleven_fields_vector_array[0]; 
        char *last_vector_array_row=variable_rows_values_eleven_fields_vector_array[(vector_size-1)]; 

        std::cout <<variable_rows_values_eleven_fields_vector_array[0] <<std::endl; 
        std::cout <<variable_rows_values_eleven_fields_vector_array[1] <<std::endl; 
        std::cout <<variable_rows_values_eleven_fields_vector_array[2] <<std::endl; 


        std::cout <<*variable_rows_values_eleven_fields_vector_array[(vector_size-1)] <<std::endl; 
        std::cout <<variable_rows_values_eleven_fields_vector_string.back()<<std::endl; 
        std::cout <<variable_rows_values_eleven_fields_vector_string.size()<<std::endl;    
        std::cout <<vector_size<<std::endl;    

        variable_rows_values_eleven_fields_vector_string.push_back(row_string); 
        std::cout <<variable_rows_values_eleven_fields_vector_array[0] [0]<<std::endl; 
        std::cout <<variable_rows_values_eleven_fields_vector_array[0] [1]<<std::endl;   
        std::cout <<variable_rows_values_eleven_fields_vector_array[0] [2]<<std::endl;   
        std::cout <<variable_rows_values_eleven_fields_vector_array[0] [3]<<std::endl;   
        std::cout <<vector_size <<std::endl; 
        std::cout <<variable_rows_values_eleven_fields_vector_array[(vector_size-1)][0] <<std::endl; 
        std::cout <<variable_rows_values_eleven_fields_vector_array[(vector_size-1)] [1]<<std::endl;    
        std::cout <<variable_rows_values_eleven_fields_vector_array[(vector_size-1)] [2]<<std::endl;    
        std::cout <<variable_rows_values_eleven_fields_vector_array[(vector_size-1)] [3]<<std::endl;    
        std::cout <<vector_size <<std::endl;    
        //fputc ('\n', stdout); 
        printf("Press any key to continue . . . "); 
        fputc ('\n', stdout); 
        _getch(); 

答えて

0

私は[] []、私は奪還したいベクトルの要素を取る2Dベクトルを作りたいですベクトルから要素をベクトル[] []を使用して単一のelemntsまたはベクトル[]を取得し、全体の行の配列を取得する 可能ですか??? これで文字列としてベクトル化する行を入力し、文字列関数を使用して区切り文字で各要素を取り出す いずれか他の方法で待っている場合は

関連する問題