samedi 19 septembre 2015

Simple SQLite Query Cursor is not finding value

For some reason in the following function the Cursor is not reading any marks. I don't know what I am doing wrong and I have been debugging this code for ours. When it runs it says the value of tagid and catid are both -1. No ping pong or pang :(

public String getCategoryNameByLawId(final int lawID){

    final String[] categoryName = {"Success"+lawID};
    final int[] tagID = {-1};
    final int[] categoryID =  {-1};

    runnable = new Runnable() {
        @Override
        public void run() {
            try {
                openToRead();

                String Query = "SELECT * from " + Constants.TABLE_LAW_TAG;
                Cursor c1 = mSqLiteDatabase.rawQuery(Query, null);

                if (c1.moveToFirst()) {
                    categoryName[0] = "ping";
                    while (c1.isAfterLast() == false) {
                        categoryName[0] = "pong";
                        try {
                            if (c1.getInt(c1.getColumnIndex(Constants.KEY_LAW_ID)) == lawID) {
                                int indexTagID = c1.getColumnIndex(Constants.KEY_TAG_ID);
                                tagID[0] = c1.getInt(indexTagID);
                                categoryName[0] = "pang";
                            }
                        } catch (Exception e) {
                            categoryName[0] = e.getMessage();
                        }
                        c1.moveToNext();
                    }
                }
                close();
                openToRead();
                Query = "SELECT * from " + Constants.TABLE_CATEGORY_TAG;
                Cursor c2 = mSqLiteDatabase.rawQuery(Query, null);
                if (c2.moveToFirst()) {
                    while (c2.isAfterLast() == false) {
                        if (c2.getInt(c2.getColumnIndex(Constants.KEY_TAG_ID)) == tagID[0]) {
                            int indexCategoryID = c2.getColumnIndex(Constants.KEY_CATEGORY_ID);
                            categoryID[0] = c2.getInt(indexCategoryID);
                        }
                        c2.moveToNext();
                    }
                }

                /*
                exceptionHandler.alert(new RuntimeException(), "catid-" + categoryID[0]);

                Query = "SELECT * from " + Constants.TABLE_CATEGORY;
                Cursor c3 = mSqLiteDatabase.rawQuery(Query, null);

                if (c3.moveToFirst()) {
                    while (c3.isAfterLast() == false) {
                        if (c3.getInt(c3.getColumnIndex(Constants.KEY_CATEGORY_ID)) == categoryID[0]) {
                            int indexCategoryName = c3.getColumnIndex(Constants.KEY_CATEGORY_NAME);
                            categoryName[0] = c3.getString(indexCategoryName);
                        }
                        c3.moveToNext();
                    }
                }
                exceptionHandler.alert(new RuntimeException(), "catnam-" + categoryName[0]);*/

                close();
            }
            catch(Exception e){
                categoryName[0] ="error";
            }
        }
    };
    new Thread(runnable).start();
    return categoryName[0].toLowerCase() + " tagid: "+ tagID[0]+ " catid: "+ categoryID[0];

}

Aucun commentaire:

Enregistrer un commentaire