Pergunta

I am having big trouble with this issue. I have my sqlite table called Category, where I put columns "name" and "limit" as text and integer. Here is some code:

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

There's model class of Category:

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




}

And there's create category and update method:

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

In spinner I have Overriden method toString from Category Class. It shows something like this below. From the left: ID, Category_Name, Category_Limit. The problem is that I set all limits to 10. Even if I use other integers, it always show '2'. Help please :)

Here's link to image: https://i.stack.imgur.com/Ae3YZ.png

Foi útil?

Solução

The insert() call returns -1 if an error occurred. I think you should check it. And make sure you write the correct column name. I tried you code,works fine!

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); 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top