My dilemma is that I am losing my ResultSet from the primary table when accessing a foreign key table. I need to save the results from the first table query while I query another table.
Scenerio:
I am implementing a recipe database system. Each recipe can have 1 to many ingredients. To resolve the 1 to many relationship, I created a separate table for the ingredients.
I have a table of recipes:
ID_Recipe: primary key, integer;
Recipe_Title: CHAR(128);
And a table of ingredients:
ID_Ingredient: PRIMARY KEY, INTEGER;
ID_Recipe: INTEGER NOT NULL;
Ingredient_Title: CHAR(64)
In my program, I have a recipe object that contains a vector of ingredients:
struct Ingredient
{
int ID;
int recipe_ID;
std::string title;
};
struct Recipe
{
int ID;
std::string title;
std::vector<Ingredient> recipe_ingredients;
};
In order to perform a for each iteration on a recipe in the table, I must load it from the database. To complete the recipe, I have to load in all of the ingredients associated with the recipe.
The problem is that when I load in the ingredients, I lose the result set of the recipes. The MySQL Connector C++ can only handle one result set at a time. I have had no luck in copying the results either (when the table size grows, I may not want to load in the entire results).
So, how can I maintain a cursor or pointer to the recipe table while I search and load from the ingredient table?
Here is what I want to do:
- For each recipe in the table do:
- Read recipe result row data into Recipe variable.
- Use recipe ID to select all ingredients where Recipe.ID_Recipe = Ingredient.ID_Recipe.
- Load results from ingredient table into vector of Recipe variable.
- Pass recipe object to call-back function.
- Advance to next recipe in the table.
- End-for
Thanks in advance for any suggestions.
(I'm using MySQL, MySQL Connector C++, Visual Studio 2008 - C++)