Reputation: 103
I have a wine database that I am trying to retrieve data from, to store in a vector and sort for structure. I can't seem to understand how to store an sql query in c++, I have the command that selects the whole table and a while loop that processes the rows but how in the world do save the results of the query. More specifically how would I store the query in a vector to sort?
Am I overthinking this? I've been pondering and testing different for hours and the logic of combining the two languages is killing me.
Can I sort a whole row? In my code can I sort row[2]
?
Can I store a whole row to a vector?
Here is my code that has a sql command to get all the wine information from a database. Then there is a while loop to display everything. How do I store everything from the sql_cmd
in a vector?
I can specify more and provide more code snippets if you need, let me know if I am still being general.
Upvotes: 0
Views: 2516
Reputation: 44284
You could do
class Wine
{
public:
string name;
string vintage;
string price; // Maybe you want to convert to double instead of storing a string
// and so on
}
vector<Wine> myWines;
In your while loop you could add:
Wine tmp;
tmp.name = row[0];
tmp.vintage = row[1];
// and so on
myWines.push_back(tmp);
Now you can sort by name like this
std::sort(myWines.begin(), myWines.end(),
[] (Wine const& a, Wine const& b) { return a.name < b.name; });
or by vintage like this
std::sort(myWines.begin(), myWines.end(),
[] (Wine const& a, Wine const& b) { return a.vintage < b.vintage; });
The sorting can be demonstrated like:
class Wine
{
public:
string name;
string vintage;
string price; // Maybe you want to convert to double instead of storing a string
// and so on
};
int main()
{
vector<Wine> myWines;
Wine tmp;
tmp.name = "d";
tmp.vintage = "3";
tmp.price = "i";
myWines.push_back(tmp);
tmp.name = "g";
tmp.vintage = "1";
tmp.price = "f";
myWines.push_back(tmp);
tmp.name = "a";
tmp.vintage = "2";
tmp.price = "c";
myWines.push_back(tmp);
cout << "Unsorted" << endl;
// Print the vector
for (auto& v : myWines)
{
cout << v.name << " " << v.vintage << " " << v.price << endl;
}
cout << "Sort by name" << endl;
std::sort(myWines.begin(), myWines.end(),
[] (Wine const& a, Wine const& b) { return a.name < b.name; });
// Print the vector
for (auto& v : myWines)
{
cout << v.name << " " << v.vintage << " " << v.price << endl;
}
cout << "Sort by vintage" << endl;
std::sort(myWines.begin(), myWines.end(),
[] (Wine const& a, Wine const& b) { return a.vintage < b.vintage; });
// Print the vector
for (auto& v : myWines)
{
cout << v.name << " " << v.vintage << " " << v.price << endl;
}
return 0;
}
output:
Unsorted
d 3 i
g 1 f
a 2 c
Sort by name
a 2 c
d 3 i
g 1 f
Sort by vintage
g 1 f
a 2 c
d 3 i
Upvotes: 3
Reputation: 27577
You want a struct
to store your data, something like:
struct Wine {
Wine(const string& name,
const string& vintage,
const string& rating,
float price,
const string& type) : name_(name), vintage_(vintage), rating_(rating), price_(price), type_(type) {};
string name_;
string vintage_;
string rating_;
float price_;
string type_;
}
vector<Wine> vec;
and then in your loop:
vec.push_back(Wine(row[0],row[1],row[2],row[3],row[4]));
Upvotes: 1