I’m having an issue where I want to create a second table within the my database. Here is the code
public class CBDataBaseHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "Recipe_Name";
public static final String KEY_CATEGORY = "Recipe_Category";
public static final String KEY_DESCRIPTION = "Recipe_Description";
public static final String KEY_ROWID2 = "_id";
public static final String fKEY_ROWID2 = "f_id";
public static final String KEY_NAME2 = "Ingredient_Name";
private static final String DATABASE_NAME = "Recipedb";
private static final String DATABASE_TABLE = "RecipeData";
private static final String DATABASE_TABLE2 = "IngredientData";
private static final int DATABASE_VERSION = 1;
private DBHelper myHelper;
private final Context mycontext;
private SQLiteDatabase mydatabase;
private static class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + "( " + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY + " TEXT NOT NULL,"
+ KEY_DESCRIPTION + " TEXT NOT NULL" + ") ");
db.execSQL("CREATE TABLE " + DATABASE_TABLE2 + "( "
+ KEY_ROWID2 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME2 + " TEXT NOT NULL, "
+ fKEY_ROWID2 + " INTEGER , FOREIGN KEY " + "(" + fKEY_ROWID2 + ") " + "REFERENCES " + DATABASE_TABLE + " ( " + KEY_ROWID + " ))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP IF EXISTS " + DATABASE_TABLE);
db.execSQL("DROP IF EXISTS " + DATABASE_TABLE2);
onCreate(db);
}
}
public CBDataBaseHelper(Context c){
mycontext = c;
}
public CBDataBaseHelper open() throws SQLException{
myHelper = new DBHelper(mycontext);
mydatabase = myHelper.getWritableDatabase();
return this;
}
public void close(){
myHelper.close();
}
public long createEntry(String name, String description, String category){
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_CATEGORY, description);
cv.put(KEY_DESCRIPTION, category);
return mydatabase.insert(DATABASE_TABLE, null, cv);
}
public long createEntry2(String name){
ContentValues cv = new ContentValues();
cv.put(KEY_NAME2, name);
return mydatabase.insert(DATABASE_TABLE2, null, cv);
}
public Cursor query() {
// Open Database
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_CATEGORY, KEY_DESCRIPTION};
Cursor cursor = mydatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
return cursor;
}
public Cursor query2() {
// Open Database
String[] columns = new String[]{KEY_ROWID2, fKEY_ROWID2, KEY_NAME2};
Cursor cursor = mydatabase.query(DATABASE_TABLE2, columns, null, null, null, null, null);
return cursor;
}
public Cursor fetchRow(long rowId) throws SQLException {
Cursor mCursor = mydatabase.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME,
KEY_CATEGORY, KEY_DESCRIPTION }, KEY_ROWID + "="
+ rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateRecipe(long rowId, String RecipeName,
String RecipeCategory, String RecipeDescription)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, RecipeName);
args.put(KEY_DESCRIPTION, RecipeDescription);
args.put(KEY_CATEGORY, RecipeCategory);
return mydatabase.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
}
I have a cursor call then to this database for the second table, this is were the error comes in, “no such table”. Any ideas?
Here is the problem, its gonna look bit silly though. The problem is the your
onCreatefor creating database is not getting called. I am sure but you can confirm that. Also youronUpgradeis not being called. The reason is DB already exists soonCreatewon’t gonna call. Now you thinkonUpgradewill be called but that wont be called as you havn’t changedDATABASE_VERSION.Resolution:
For adb use
rm <db_name>Other resolution if some data is already there (that you want to keep for testing), this only for testing purposes:
onUpgradeto be called so write your sqls so that you dont loose data. But obviously your productiononUpgradewill be different from this one.