On a form, I have a Listbox, multiline textbox, and a wxGrid. Same as mentioned in my prior post, this is a little program for my girlfriend, one for all of her recipes, etc.
The Listbox will hold the recipe names, textbox the recipe note, and the grid will house the ingredients/amounts.
I'm able to load at startup the recipe names in the Listbox, and the corresponding note and ingredients/amounts.
My problem is that I can't figure out how to change the note and ingredients/amount when the Listbox selection changes to a new recipe name. Here is the code I'm using for this:
Code: Select all
void Angela001Frame::OnListBox1Select(wxCommandEvent& event)
{
try
{
db->Open("zyx.db");
wxSQLite3Statement stmt1 = db->PrepareStatement(" SELECT recipe_id, recipe_name, note FROM recipes WHERE recipe_name = ?");
stmt1.Bind(1, ListBox1->GetSelection()); // I'M TRYING TO BIND THE LISTBOX recipe_name HERE
wxSQLite3ResultSet result = stmt1.ExecuteQuery();
//int colCount = result.GetColumnCount();
int set_id = result.GetInt(0);
int newRow = 0;
wxMessageBox("while loop next","");
while (result.NextRow())
{
wxMessageBox("Inside while loop", ""); // I NEVER MAKE IT THIS FAR
ListBox1->AppendAndEnsureVisible(result.GetAsString(1));
if (newRow > 0){
newRow++;
} else {
ListBox1->SetSelection(0);
TextCtrl1->SetValue(result.GetAsString(2));
wxSQLite3Statement stmt2 = db->PrepareStatement("SELECT ingredient, amount "
"FROM recipe_ingredients "
"INNER JOIN recipes ON recipes.recipe_id = recipe_ingredients.recipe_id "
"WHERE recipe_ingredients.recipe_id = ?");
stmt2.Bind(1, set_id);
wxSQLite3ResultSet result1 = stmt2.ExecuteQuery();
int colCount1 = result1.GetColumnCount();
while (result1.NextRow())
{
for (int i = 0; i < colCount1; i++)
{
Grid_View_Recipes->SetCellValue(newRow, i, result1.GetAsString(i));
}
newRow++;
}
stmt2.Finalize();
}
}
stmt1.Finalize();
} catch(wxSQLite3Exception& e) {
}
db->Close();
}
Here are my two tables:
Code: Select all
CREATE TABLE recipes(
recipe_id INTEGER PRIMARY KEY AUTOINCREMENT,
recipe_name TEXT,
note TEXT);
Code: Select all
CREATE TABLE recipe_ingredients(
ingredient_id INTEGER PRIMARY KEY AUTOINCREMENT,
recipe_id INTEGER,
ingredient TEXT,
amount INTEGER,
FOREIGN KEY(recipe_id) REFERENCES recipes(recipe_id) ON DELETE CASCADE);
And help on this would be cool. Thanks.