Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

Android SQLite cursoroutofboundsexception

Problem with getting an object (getRecipe() method) from the SQLite as I get an cursoroutofbounds exception.

Here is the code:

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.apps.database.sqlite.model.Ingredients;
import com.apps.database.sqlite.model.Photo;
import com.apps.database.sqlite.model.Recipe;

public class RecipeDbHelper extends SQLiteOpenHelper {
    private static final String LOG = "DatabaseHelper";
    private static final String DATABASE_NAME = "RecipeManager";
    private static final int DATABASE_VERSION = 11;
    private static final String TABLE_RECIPE = "recipe";
    private static final String TABLE_INGREDIENT = "ingredients";
    private static final String TABLE_PHOTO = "photo";
    private static final String TABLE_PROCESSING = "processing";
    private static final String KEY_ID = "recipe_id";

    public RecipeDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        for (int i = 0; i < createTables().length; i++) {
            db.execSQL(createTables()[i]);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECIPE);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_INGREDIENT);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PHOTO);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROCESSING);

        // create new tables
        onCreate(db);

    }

    private String[] createTables() {
        String[] tables = new String[4];

        final String CREATE_TABLE_RECIPE = "CREATE TABLE " + TABLE_RECIPE + "("
                + KEY_ID + " INTEGER PRIMARY KEY, title TEXT)";
        final String CREATE_TABLE_INGREDIENTS = "CREATE TABLE "
                + TABLE_INGREDIENT + "(" + KEY_ID + " INTEGER REFERENCES "
                + TABLE_RECIPE + ", foodname TEXT, "
                + "amount real, measurement String, " + "PRIMARY KEY(" + KEY_ID
                + ", foodname))";
        final String CREATE_TABLE_PHOTO = "CREATE TABLE " + TABLE_PHOTO + "("
                + KEY_ID + " INTEGER PRIMARY KEY REFERENCES " + TABLE_RECIPE
                + ", pathfile TEXT)";

        final String CREATE_TABLE_PROCESSING = "CREATE TABLE "
                + TABLE_PROCESSING + "( " + KEY_ID
                + " INTEGER PRIMARY KEY, description TEXT)";

        tables[0] = CREATE_TABLE_RECIPE;
        tables[1] = CREATE_TABLE_INGREDIENTS;
        tables[2] = CREATE_TABLE_PHOTO;
        tables[3] = CREATE_TABLE_PROCESSING;
        return tables;
    }

    public boolean addRecipe(Recipe recipe) {

        for (Recipe rep : getAllRecipes()) {
            if (recipe.getTitle().equals(rep.getTitle())) {
                return false;
            }
        }
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues value = new ContentValues();

        value.put(KEY_ID, recipe.getRecipe_id());
        value.put("title", recipe.getTitle());
        db.insert(TABLE_RECIPE, null, value);
        db.close();
        return true;

    }

    public Recipe getRecipe(String name) {
        Recipe recipe = null;
        String KEY_TITLE = "title";
        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT * FROM " + TABLE_RECIPE
                + " WHERE "+ KEY_TITLE +" = '" + name + "'";
        Cursor cursor = db.rawQuery(selectQuery, null);
        recipe = new Recipe(cursor.getColumnIndex(KEY_ID),
                cursor.getString(cursor.getColumnIndex(KEY_TITLE)));

        cursor.close();
        return recipe;


          //REST OF THE CODE NOT INCLUDED
    }
}

Any tips would be really good! I am really stuck on the exception I get from the getRecipe() method.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In getRecipe()

 Cursor cursor = db.rawQuery(selectQuery, null); 
 if(cursor.moveToFirst())
 {
 recipe = new Recipe(cursor.getColumnIndex(KEY_ID),
 cursor.getString(cursor.getColumnIndex(KEY_TITLE))); 
 cursor.close();
 return recipe; 
 }
  return null;

Reference:

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#rawQuery(java.lang.String, java.lang.String[])

As suggested by Selvin and after re-checking docs

rawQuery returns a Cursor object, which is positioned before the first entry. So cursor is not null.

Also

public abstract boolean moveToFirst ()

Added in API level 1
Move the cursor to the first row.

This method will return false if the cursor is empty.

So if cursor is not empty its returns true else returns false. So there is really no need to check if cursor is null as i first posted.

Similar posts

Android sqlite db.query leads to CursorIndexOutOfBoundsException


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...