SQLite没有创建第二个表

时间:2011-12-09 21:46:57

标签: android sqlite

我遇到了一个问题,我想在我的数据库中创建第二个表。这是代码

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

我有一个游标调用然后到第二个表的这个数据库,这是错误进来,“没有这样的表”。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

这是问题,但它看起来有点傻。问题是您的onCreate创建数据库没有被调用。我相信你可以确认一下。此外,您的onUpgrade未被调用。原因是数据库已经存在,因此onCreate将无法调用。现在你认为onUpgrade会被调用,但是因为你没有改变DATABASE_VERSION而不会被调用。

分辨率:

  • 使用命令实用程序'adb shell`或使用文件资源管理器通过eclipse删除数据库。数据库位于“/ data / data // databases”下。

对于adb使用rm <db_name>

其他解决方案,如果某些数据已存在(您希望保留用于测试),这仅用于测试目的:

  • 尝试将DATABASE_VERSION更改为其当前值以外的值。这将导致你的onUpgrade被调用,所以写你的sqls,这样你就不会丢失数据。但很明显,您的作品onUpgrade将与此不同。

答案 1 :(得分:2)

好吧,如果这仅用于测试目的而不是高效代码,那么这一行可以帮助您确保每次启动应用程序时都创建数据库。 (因为你的问题很可能与onCreate根本没有被调用有关,而是onUpgrade而不是....只是猜测,设置一个断点,然后你肯定知道)

context.deleteDatabase(DATABASE_NAME);

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