Ich mache eine mysql-app mit C++.
Ich möchte einen Code machen, der mir die erforderliche Zeile bringt.
Dann nehme ich den Wert dieser Zeile.
Ich lege es in das erste Array und iteriere durch die Spalten, bis diese Zeile fertig ist.
Dann bekomme ich die nächste Zeile in Array2.
Dann habe ich das ganze Array1 in Vektor des Typs Array.
Ich mache das, weil ich die nächsten Zeilen durchlaufen werde und die neuen Zeilenwerte in die gleichen 2 Arrays setze, aber ich muss die alten ausgewählten Zeilen im Vektor behalten, die sich nach der Zeit vergrößern.
Ich habe viele Möglichkeiten ausprobiert, von denen ich denke, es gibt Zeiger im Vektor nicht die echten Daten von Array.
Wenn ich versuche, alte Vektorwerte zu bekommen finde ich die jüngsten
dies der Code:Vektor mit Array fester Länge aus MySQL-Zeile [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();