dimanche 13 septembre 2015

Cant update sqlite android

My first time posting a question in here.

I write my app and I want to have a feature where user long click on the item, they can choose either to edit or delete that item. The delete function works fine. But when they choose to edit, instead of editing and saving, it just delete the item. I've been working a few days but still cant find what's wrong.

My data base class:

public class DBAdapter {

private static final String TAG = "DBAdapter";

public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;

public static final String KEY_ACCOUNT = "account";
public static final String KEY_DATE = "date";
public static final String KEY_NOTE = "note";
public static final String KEY_ENTRY = "entry";
public static final String KEY_OTHERS = "others";

public static final int COL_ACCOUNT = 1;
public static final int COL_DATE = 2;
public static final int COL_NOTE = 3;
public static final int COL_ENTRY = 4;
public static final int COL_OTHERS = 5;

public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_ACCOUNT, KEY_DATE, KEY_NOTE, KEY_ENTRY, KEY_OTHERS};

public static final String DATABASE_NAME = "MyDb";
public static final String DATABASE_TABLE = "mainTable";

public static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE_SQL =
        "create table " + DATABASE_TABLE
                + " (" + KEY_ROWID + " integer primary key autoincrement, "

                + KEY_ACCOUNT + " text, "
                + KEY_DATE + " text not null, "
                + KEY_NOTE + " text, "
                + KEY_ENTRY + " real not null, "
                + KEY_OTHERS + " text "
                + ");";

private final Context context;

private DatabaseHelper myDBHelper;
private SQLiteDatabase db;

public DBAdapter open() {
    db = myDBHelper.getWritableDatabase();
    return this;
}

public long insertRow(String account, String date, String note, double entry, String others) {

    ContentValues values = new ContentValues();
    values.put(KEY_ACCOUNT, account);
    values.put(KEY_DATE, date);
    values.put(KEY_NOTE, note);
    values.put(KEY_ENTRY, entry);
    values.put(KEY_OTHERS, others);

    return db.insert(DATABASE_TABLE, null, values);
}

public boolean deleteRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    return db.delete(DATABASE_TABLE, where, null) != 0;
}

public boolean updateRow(long id, String account, String date, String note, double entry, String others) {
    String where = KEY_ROWID + "=" + id;

    ContentValues values = new ContentValues();
    values.put(KEY_ACCOUNT, account);
    values.put(KEY_DATE, date);
    values.put(KEY_NOTE, note);
    values.put(KEY_ENTRY, entry);
    values.put(KEY_OTHERS, others);

    return db.update(DATABASE_TABLE, values, where, null) != 0;
}

And my code at my Activity:

    myDb = new DBAdapter(this);
    myDb.open();
    myDb.updateRow(id,a,d,n,e,c);

Instead of updating the data, it delete the whole row.

Do you know what did I do wrong?

Aucun commentaire:

Enregistrer un commentaire