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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…