2015-05-20 205 views
-1

我正在使用SQLite,並且在刪除數據時遇到問題。
首先,這是我如何將數據添加到數據庫:從SQLite中刪除數據數據庫不起作用

public void addRecipe (QueryVars Recipee){ 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_Recipe, Recipee.getRecipe()); 

    db.insert(TABLE_Recipes, null, values); 
    db.close(); 
} 

這就是我如何從數據庫中獲取數據:

public List<QueryVars> getAllBooks() { 
    List<QueryVars> recipes = new LinkedList<QueryVars>(); 

    String query = "SELECT * FROM " + TABLE_Recipes; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(query, null); 

    QueryVars Recipe = null; 
    if (cursor.moveToFirst()) { 
     do { 
      Recipe = new QueryVars(); 
      // Recipe.setId(Integer.parseInt(cursor.getString(0))); 
      Recipe.setRecipe(cursor.getString(1)); 
      recipes.add(Recipe); 
     } while (cursor.moveToNext()); 
    } 

    return recipes; 
} 

保存和查詢數據是工作完全正常,但是當我嘗試用下面的代碼刪除行時,它不起作用。

public void deleteRecipes(QueryVars Recipe) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_Recipes, KEY_ID + " = ?", new String[] { String.valueOf(Recipe.getId()) }); 
    db.close(); 
} 

這是我用來創建表的查詢:

private static final String CREATE_BOOK_TABLE = 
     "CREATE TABLE Recipes (" 
      + "id INTEGER PRIMARY KEY AUTOINCREMENT, " 
      + "Recipe TEXT" 
     + ")"; 

而且常量我在我的代碼使用上面都是這樣定義的:

private static final TABLE_Recipes = "Recipes"; 
private static final String KEY_ID = "id"; 
private static final String KEY_Recipe = "Recipe"; 
private static final String[] COLUMNS = {KEY_ID,KEY_Recipe}; 

回答

0

您的代碼中有兩個問題可能是錯誤的根源,但兩者都有相同的原因:您讓SQLite生成您的Recipe對象的ID,但您從不將該ID設置爲對象。

  1. 當你添加了一些數據庫add()方法返回這是該行生成的ID。您可以將此ID設置爲Recipe對象,否則Recipe對象在您從數據庫重新加載之前不會有ID。

    public void addRecipe (QueryVars Recipee){ 
        SQLiteDatabase db = this.getWritableDatabase(); 
    
        ContentValues values = new ContentValues(); 
        values.put(KEY_Recipe, Recipee.getRecipe()); 
    
        final long id = db.insert(TABLE_Recipes, null, values); 
        Recipee.setId(id); 
    
        db.close(); 
    } 
    
  2. 當你從數據庫中讀取的Recipe對象,你不設置值的對象上的ID,所以你從數據庫中讀取無Recipe對象都有一個ID,這意味着你不能從數據庫中刪除。此修復程序又是非常簡單的:

    public List<QueryVars> getAllBooks() { 
        List<QueryVars> recipes = new LinkedList<QueryVars>(); 
    
        String query = "SELECT * FROM " + TABLE_Recipes; 
    
        SQLiteDatabase db = this.getWritableDatabase(); 
        Cursor cursor = db.rawQuery(query, null); 
    
        final int idIndex = cursor.getColumnIndex(KEY_ID); 
        final int recipeIndex = cursor.getColumnIndex(KEY_Recipe); 
    
        QueryVars Recipe = null; 
        if (cursor.moveToFirst()) { 
         do { 
          Recipe = new QueryVars(); 
          Recipe.setId(cursor.getLong(idIndex)); 
          Recipe.setRecipe(cursor.getString(recipeIndex)); 
          recipes.add(Recipe); 
         } while (cursor.moveToNext()); 
        } 
    
        return recipes; 
    } 
    

    這使用getColumnIndex()可靠地獲得每列的正確索引,然後讀取ID和光標的配方,並將其設置爲Recipe對象。


請注意Recipe對象需要有一個long ID! int ID與SQLiteDatabase不兼容!

0

你不捕獲數據庫生成的食譜ID,ID爲零。它不匹配刪除中的任何行。

取消對

// Recipe.setId(Integer.parseInt(cursor.getString(0))); 

(考慮使用cursor.getInt()代替)

也可能存儲insert()的返回值作爲配方ID。