Android SQLite行没有更新

时间:2014-05-11 14:27:37

标签: java android database sqlite

我在这个问题上遇到了大麻烦。我有我的sqlite表名为Category,我在其中放置了列" name"和"限制"作为文本和整数。这是一些代码:

   private static final String CREATE_TABLE_CATEGORY = "CREATE TABLE "
            + TABLE_CATEGORIES + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_CATEGORY_NAME
            + " TEXT," + KEY_SET_LIMIT + " INTEGER" + ")";

有类别的模型类:

public class Category {

int id, limit;
String categoryName;

// Constructors

public Category(){

}

public Category(String categoryName, int limit){
    this.categoryName = categoryName;
    this.limit = limit;
}

//Setters

public void setCategoryName(String categoryName){
    this.categoryName = categoryName;
}

public void setId(int id){
    this.id = id;
}

public void setLimit(int limit) {
    this.limit = limit;
}

// Getters

public String getCategoryName(){
    return this.categoryName;
}

public int getId(){
    return this.id;
}

public int getLimit() {
    return this.limit;
}

@Override
 public String toString(){
    return getId() + " " + getCategoryName() + " " + getLimit();
}




}

并且创建了类别和更新方法:

public long createCategory(Category cat) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_CATEGORY_NAME, cat.getCategoryName());
    values.put(KEY_SET_LIMIT, cat.getLimit());


    // insert row
    long cat_id = db.insert(TABLE_CATEGORIES, null, values);

    return cat_id;
}

public int updateCategoryLimit(long cat_id, int limit) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_SET_LIMIT, limit);


    // updating row
    return db.update(TABLE_CATEGORIES, values, KEY_ID + " = ?",
            new String[] { String.valueOf(cat_id) });
}

在微调器中,我有来自Category Class的Overriden方法toString。它显示如下所示。从左侧:ID,Category_Name,Category_Limit。问题是我将所有限制设置为10.即使我使用其他整数,它也总是显示' 2'。请帮助:)

此处链接到图片:http://i.stack.imgur.com/Ae3YZ.png

1 个答案:

答案 0 :(得分:1)

如果发生错误,insert()调用返回-1。我想你应该检查一下。 并确保您编写正确的列名称。 我试过你的代码,工作正常!

05-11 23:13:14.110:I / db update s =(30650):s = 1

 ContentValues values = new ContentValues();
        values.put(KEY_SET_LIMIT, 29);
       int s= db.update(TABLE_CATEGORIES, values, KEY_ID + " = ?", new String[] { String.valueOf(2) });
       Log.i("db update s=", "s= " +s);