aspiringCoder
aspiringCoder

Reputation: 415

SQLite not creating the second table

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?

Upvotes: 1

Views: 2118

Answers (2)

Sgali
Sgali

Reputation: 372

Well, if this is only for testing purposes and not productive code, then this line might help you make sure that your db is being created each time you start your app. (as your problem is most likely related to the onCreate not being called at all, but onUpgrade instead....just a guess though, set a breakpoint, then you know for sure)

context.deleteDatabase(DATABASE_NAME);

   public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        context.deleteDatabase(DATABASE_NAME); 
    }

Upvotes: 2

havexz
havexz

Reputation: 9590

Here is the problem, its gonna look bit silly though. The problem is the your onCreate for creating database is not getting called. I am sure but you can confirm that. Also your onUpgrade is not being called. The reason is DB already exists so onCreate won't gonna call. Now you think onUpgrade will be called but that wont be called as you havn't changed DATABASE_VERSION.

Resolution:

  • Remove the database either by command utility 'adb shell` or via eclipse using the file explorer. The database resides in under '/data/data//databases'.

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:

  • try changing DATABASE_VERSION to something other than what its current value is. This will cause your onUpgrade to be called so write your sqls so that you dont loose data. But obviously your production onUpgrade will be different from this one.

Upvotes: 2

Related Questions