我在数据库中创建第二个表时遇到问题。这是代码

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;
            }
        }


我有一个游标调用,然后访问了该数据库的第二个表,这是错误的出现,“没有这样的表”。有任何想法吗?

最佳答案

这是问题所在,尽管看起来有点傻。问题是您的创建数据库的onCreate没有被调用。我确定,但是您可以确认。同样,您的onUpgrade也没有被调用。原因是数据库已经存在,所以onCreate不会调用。现在您认为onUpgrade将被调用,但由于您未更改DATABASE_VERSION而不会被调用。

解析度:


通过命令实用程序“ adb shell”或使用文件浏览器通过eclipse删除数据库。该数据库位于“ / data / data // databases”下。


对于adb使用rm <db_name>

如果已经有一些数据(要保留以进行测试),则为其他解决方案,仅用于测试目的:


尝试将DATABASE_VERSION更改为当前值以外的其他值。这将导致您的onUpgrade被调用,因此编写您的sql,以免丢失数据。但是显然您的产量onUpgrade会与此不同。

关于android - SQLite不创建第二个表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8452113/

10-08 21:17