Skip to content Skip to sidebar Skip to footer

Rawquery Not Working In Sqlite Android

All i want to do is set a string equal to one of the entries in my database. The table i am accessing has 1 column (besides the ID column). I have been struggling with this for a

Solution 1:

Try this,

public String getCategory(int catId){
        Cursormycursor= myDatabase.rawQuery("SELECT " + COLUMN_CATEGORIES  + " FROM "+ StringCategory_Table +" WHERE " + _ID + " = " + catId, null);
      if(mycursor.moveToFirst()){ //Edited based on suggestion from SAMStringstrCatName= mycursor.getString(mycursor.getColumnIndex(COLUMN_CATEGORIES));
                  return strCatName;
        } else {
        returnnull;
         }


        }

Solution 2:

It looks like you have simply forgot to move the Cursor to the first result. Try this instead:

public String getCategory(int catId){
    Cursormycursor= myDatabase.rawQuery("SELECT " + COLUMN_CATEGORIES  + " FROM " + StringCategory_Table +" WHERE " + _ID + " = " + catId, null);

    // Check if the row exists, return it if it doesif(mycursor.moveToFirst())
        return mycursor.getString(mycursor.getColumnIndex(COLUMN_CATEGORIES));

    // No rows match idreturn"";
}

Solution 3:

Solution 4:

Looks like your select statement is probably wrong. You should pass the column name string instead of "COLUMN_CATEGORIES" which looks like an integer. After that you should use the commented out way of accessing the string.

Post a Comment for "Rawquery Not Working In Sqlite Android"