包含从mysql行构建的固定长度数组的向量[x]

时间:2016-05-25 10:56:17

标签: c++ mysql arrays vector

我正在使用c ++创建一个mysql应用程序 我想制作一个能够获得所需行的代码 然后我取这一行的价值 我把它放在第一个数组中并遍历列,直到这一行结束 然后我得到下一行把它放在array2中 然后我把整个array1放在数组类型的向量中 我之所以这样做,是因为我将遍历下一行并将新行值放在相同的2个数组中,但我需要在向量中保留旧的选定行,这些行的大小会随时间增加。
我尝试了其中一种方法,我认为向量中有指针而不是数组的实际数据 当我尝试获取旧的矢量值时,我发现最近的一个 这是代码:

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();

1 个答案:

答案 0 :(得分:0)

简而言之,我想制作2d矢量,它通过vector [] []获取元素然后我想从vector使用vector [] []重新获取元素以获得单个elemnts或vector []以获取整行的数组 可能吗??? 现在我只是输入行作为字符串向量,然后使用字符串函数通过分隔符检索每个元素 如果有人有其他方式我等待