How to read a table from SQL Server 2008 to a a vector in C++ in the following way:
SQL TABLE 2008 table
1att 2att 3att 4att 5att 6att 7att ... attn
--------------------------------------------
565 526 472 527 483 529 476 470 502
497 491 483 488 488 483 496 515 491
467 516 480 477 494 497 478 519 471
488 466 547 498 477 466 475 480 516
Take every odd column into a only vector…
array[565, 497, 467, 488, //1att column
472, 483, 480, 547, //3att column
483, 488, 494, 477, //5att column
...
502, 491, 471, 516, //Last odd att column
]
I am using:
#include <vector>
size_t size = 10; //HOW TO GET THE SIZE
std::vector<int> array(size); // make room for ,
// and initialize them to 0
for(int i=0; i<size; ++i){
array[i] = i;
}
...
So my main problem is how to communicate c++ with SQL, and then read the table into the vector… I am using Visual Studio 2008
There are many ways to do this:
database
Fetching Single Columns
In this method, you tell the data base to select one column. You fill in a single vector by iterating through the result set. Repeat for each column you are interested in, using different vectors for each column.
Fetching odd columns from database
Tell database to return all the columns you are interested in (use many columns in the SELECT statement). Read a record from the result set. Put append values from the fields into appropriate vectors. Repeat reading & extraction for each record in the result set.
Fetching entire table from database.
Similar to above. Tell database to fetch all columns. While there is a result set record, copy values from interested fields into appropriate vectors.
The latter method may be fastest since the database is returning rows from the table without filtering. The other methods require the database to create a new result set of records, filtering from the existing table. The fastest algorithm is obtained by profiling.
Edit 1: Sample Code
I don’t use SQL Server 2008, but MySQL. So this code will not work exactly for your purposes.