vendredi 17 juin 2016

android database checking multiple cursors containing different rawQueries

Ok here a short explanation: I have a listview showing my database entries. When I click on an entry it gets deleted. The entries can contain 3 different values meaning for example an entry can have all 3 values in it or only just one or two and so on.

Now I want to check in the delete-method which of those 3 values are present in the deleted entry.

For this I created 7 cursors each with a different rawQuery for my needs. After that I make a check with an if-else statement which cursor fits.

And here comes the problem: Only my first if statement is excecuted, the others are not checked.

I tried cursor.movetofirst(), cursor != null and cursor != null && cursor.getCount()>0

What am I missing?

Cursor cursor1 = database.rawQuery("SELECT _id, librust FROM " + VorgangDbHelper.TABLE_VORGAENGE_LIST + " WHERE _id = ? AND librust = ?", new String[]{s_deleteId,mcontext.getResources().getString(R.string.leftbreast)});
Cursor cursor2 = database.rawQuery("SELECT _id, rebrust FROM " + VorgangDbHelper.TABLE_VORGAENGE_LIST + " WHERE _id = ? AND rebrust = ?", new String[]{s_deleteId,mcontext.getResources().getString(R.string.rightbreast)});
Cursor cursor3 = database.rawQuery("SELECT _id, bottle FROM " + VorgangDbHelper.TABLE_VORGAENGE_LIST + " WHERE _id = ? AND bottle = ?" , new String[]{s_deleteId,mcontext.getResources().getString(R.string.babybottle)});
Cursor cursor4 = database.rawQuery("SELECT _id, librust, rebrust, bottle FROM " + VorgangDbHelper.TABLE_VORGAENGE_LIST + " WHERE _id = ? AND librust = ? AND rebrust = ? AND bottle = ?" , new String[]{s_deleteId,mcontext.getResources().getString(R.string.leftbreast),mcontext.getResources().getString(R.string.rightbreast),mcontext.getResources().getString(R.string.babybottle)});
Cursor cursor5 = database.rawQuery("SELECT _id, librust, rebrust FROM " + VorgangDbHelper.TABLE_VORGAENGE_LIST + " WHERE _id=? AND librust=? AND rebrust=?", new String[]{s_deleteId,mcontext.getResources().getString(R.string.leftbreast),mcontext.getResources().getString(R.string.rightbreast)});
Cursor cursor6 = database.rawQuery("SELECT _id, librust, bottle FROM " + VorgangDbHelper.TABLE_VORGAENGE_LIST + " WHERE _id = ? AND librust = ? AND bottle = ?", new String[]{s_deleteId,mcontext.getResources().getString(R.string.leftbreast),mcontext.getResources().getString(R.string.babybottle)});
Cursor cursor7 = database.rawQuery("SELECT _id, rebrust, bottle FROM " + VorgangDbHelper.TABLE_VORGAENGE_LIST + " WHERE _id = ? AND rebrust = ? AND bottle = ?", new String[]{s_deleteId,mcontext.getResources().getString(R.string.rightbreast),mcontext.getResources().getString(R.string.babybottle)});

If-else

if (cursor1.moveToFirst())
        {
            //do sth;
        }
        else if (cursor2.moveToFirst())
        {
            //do sth;
        }
        else if (cursor3.moveToFirst())
        {
            //do sth;
        }
        else if (cursor4.moveToFirst())
        {
            //do sth;
        }
        else if (cursor5.moveToFirst())
        {
            //do sth;
        }
        else if (cursor6.moveToFirst())
        {
            //do sth;
        }
        else if (cursor7.moveToFirst())
        {

        }
        cursor1.close();
        cursor2.close();
        cursor3.close();
        cursor4.close();
        cursor5.close();
        cursor6.close();
        cursor7.close();

Aucun commentaire:

Enregistrer un commentaire