0

so i have a database with a table which has an columns(COLUMN_ID, COLUMN_TARGET, COLUMN_CURRENT). i am trying to retrieve the value of column COLUMN_TARGET where COLUMN_ID =1 and save it in variable tar and return it but my app keeps crashing when i run it. I used log messeges to work out where it goes wrong and it prints out "1..." and then crashes.

public int getTarget() {

    SQLiteDatabase db = this.getReadableDatabase();
    String[] columns = {COLUMN_TARGET};
    int tar=0;
    Log.d("tag","1...\n");
    Cursor c = db.query(TABLE_VALUES, columns, COLUMN_ID+"=?", new String[] { String.valueOf(1) }, null, null, null,null );
    Log.d("tag","2...\n");
    if (c.getCount() == 1) {
        Log.d("tag","3...\n");
        c.moveToFirst();
    }
    Log.d("tag","4...\n");
        tar = c.getInt(0);
    Log.d("tag","5...\n");
    db.close();
    c.close();
    Log.d("tag","6...\n");
   // return Integer.parseInt(tar);
    return tar;

}

can someone plz fix this for me?! thanks in advance

Csbk
  • 49
  • 6

1 Answers1

0

Try below:

    String SELECT_SQL = "SELECT * FROM "+ TABLE_VALUES + "WHERE COLUMN_ID =" + 1 + ";";
    Cursor c = db.rawQuery(SELECT_SQL, null);
    if (c.getCount() == 1) {
       Log.d("tag","3...\n");
       c.moveToFirst();
       int tar = c.getInt(0);
    }
Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61