Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having difficulty understanding the issue with the code here as described in the logcat. I believe it might be something to do with the id variable in the createPlan method needs to come from the id created by the autoincrement of the createPlanRecipe method but uncertain how to implement that. I don't think there was anything wrong with the database I've checked the formatting a hundred times, but can add it here if need be. Thanks in advance.
Tried using insert_last_rowid, but this doesn't work either.
With regards to this line Cursor c = db.rawQuery("selectlast_insert_rowid()",null);
c.moveToFirst();
How do I get the id created from db.createPlanRecipe(d_name, dayOfTheWeek, recipe_name); and use this as the plRecipe variable in db.execSQL(getInsertPlan(plan_name, plRecipe));

What I have tried:

public class CreateMealPlan extends MainActivity {

DatePicker datepicker;
Button submit;
List<com.stu54259.plan2cook.Model.Category> listRecipe = new ArrayList<>();
Cursor c;
RecyclerView recipeList;
RecipeListAdapter adapterRecipe;
String recipe_name;
EditText editPlanName;
Integer id;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_meal_plan);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.home:
                    Intent a = new Intent(CreateMealPlan.this,MainActivity.class);
                    startActivity(a);
                    break;
                case R.id.recipes:
                    Intent b = new Intent(CreateMealPlan.this,RecipeSearch.class);
                    startActivity(b);
                    break;
                /*case R.id.shoppingList:
                    Intent c = new Intent(CreateMealPlan.this, ShoppingList.class);
                    startActivity(c);
                    break;*/
                case R.id.mealPlan:
                    Intent d = new Intent(CreateMealPlan.this, MenuPlan.class);
                    startActivity(d);
                    break;
                /*case R.id.reminder:
                    Intent e = new Intent(CreateMealPlan.this, Reminder.class);
                    startActivity(e);
                    break*/
            }
            return false;
        }
    });
    datepicker = findViewById(R.id.calendarView);
    ListRecipes();
    RecipeListAdapter.OnRecipeClickListener listener = new RecipeListAdapter.OnRecipeClickListener() {
        public void onRecipeClicked(int position, String recName) {
            Log.d("Recipe selected",  recName);
            recipe_name = recName;
        }

    };
    adapterRecipe = new RecipeListAdapter(this, listRecipe, listener);
    recipeList = findViewById(R.id.recipes);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this,
            LinearLayoutManager.VERTICAL, false);
    recipeList.setLayoutManager(mLayoutManager);
    recipeList.setItemAnimator(new DefaultItemAnimator());
    recipeList.setAdapter(adapterRecipe);

    submit = (Button) findViewById(R.id.create);

    // perform click event on submit button
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CreatePlan();

        }

        });
}
public void ListRecipes() {
    listRecipe.clear();
    SQLiteDatabase db = (new DatabaseManager(this).getWritableDatabase());
    String selectQuery = " SELECT recipe_name, image, image2, category" + " FROM " + DatabaseManager.TABLE_RECIPE + "  GROUP BY recipe_name";
    c = db.rawQuery(selectQuery, null);
    Log.d("Query", selectQuery);
    if (c.moveToFirst()) {
        do {
            com.stu54259.plan2cook.Model.Category category = new com.stu54259.plan2cook.Model.Category();
            category.setRecipe_name(c.getString(c.getColumnIndex("recipe_name")));
            category.setImage(c.getInt(c.getColumnIndex("image")));
            category.setImage2(c.getString(c.getColumnIndex("image2")));
            category.setCategory_name(c.getString(c.getColumnIndex("category")));
            listRecipe.add(category);
        } while (c.moveToNext());
        c.close();
    }

}
public void CreatePlan(){
    editPlanName = findViewById(R.id.editPlanName);
    String plan_name = editPlanName.getText().toString();
    DatabaseManager db;
    int day = datepicker.getDayOfMonth();
    int month = datepicker.getMonth();
    int year = datepicker.getYear();
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
    Integer d_name = day;
    Log.d("Date", String.valueOf(d_name));
    String dayOfTheWeek = sdf.format(d_name);
    String date = day + "/" + month + "/" +year;
    db = new DatabaseManager(getApplicationContext());
    Log.d("Recipe name", recipe_name);
    db.createPlanRecipe(d_name, dayOfTheWeek, recipe_name);
    db.createPlan(plan_name, id);

}



public void createPlan(String plan_name, Integer plRecipe) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c = db.rawQuery("select last_insert_rowid()",null);
    c.moveToFirst();

    if (c != null && (c.getCount() > 0)) {plRecipe = c.getInt(c.getColumnIndex("id"));}
    db.execSQL(getInsertPlan(plan_name, plRecipe));
}
Posted
Updated 1-Oct-20 4:05am
v2
Comments
Richard Deeming 1-Oct-20 4:37am    
You seem to have forgotten to tell us what the issue is.

Click the green "improve question" link and update your question to include the full details of the error. Remember to indicate which line of code it relates to.
David Crow 1-Oct-20 9:06am    
I use something similar to the following:
DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(this);
SQLiteDatabase db = databaseOpenHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("CustNum", m_customerInfo.m_nCustNum);
Calendar calNow = Calendar.getInstance();
cv.put("OrderDate", (double) calNow.getTimeInMillis() / 1000.0);
cv.put("Status", OrderInfo.STATUS_NEW);
long lOrderNum = db.insert("Orders", null, cv);
Log.d("Test", "Added order " + lOrderNum);
It's not needed to solve your problem, but DatabaseOpenHelper is derived from SQLiteOpenHelper, and I explain it a bit further here.

1 solution

If you want to get the "last" (auto-incrementing) ID, use: SELECT MAX(ID) ...

c# - How to find Max element in SQLite? - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900