简体   繁体   中英

Android: db.update() issue

how do I update all row in a table ? In my code, I use a non-primary key stat for some matters and I found it hard to understand why can't I solve this little problem I have.

Here is my table:

______________________________________________
|   _id   |   Pname   |   escr   |   stat    |
|_________|___________|__________|___________|
|    1    |   Andrew  |    15    |     0     |
|    2    |    John   |    12    |     0     |
|    3    |    Ian    |    10    |     1     |

In my table, I need to make the column stat of all entries into 0 . So I made my code like this;

    public int ClearRecentPlayer(ClearRecPlayerData pd) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues v = new ContentValues();
        v.put(COLUMN_NAME_PSTATS, pd.getRest());

        return db.update(TABLE_NAME_PLAYER, v, null,null);
        }

//"UPDATE player SET stat =?" right??

Then I called my method;

.
.
.
DataConn myDb = new DataConn(this);
myDb.ClearRecentPlayer(new ClearRecPlayerData(0));
.
.
.

ClearRecPlayerData.java

public class ClearRecPlayerData {

    int _rest;

    public ClearRecPlayerData(){
    }

    public ClearRecPlayerData(int rest){
        this._rest = rest;
    }

    public int getRest(){

        return this._rest;
    }
    public void setRest(int rest){

        this._rest = rest;
    }
}

and then after all of these, I found out it has done nothing at all. Not even what I expected. :-(

EDIT: after that update, I made another class;

myDb.RegisterPlayer(new Playerdata(passPname,0,1)); //inserts perfectly fine

that will insert a new row in my table;

    ______________________________________________
    |   _id   |   Pname   |   escr   |   stat    |
    |_________|___________|__________|___________|
    |    1    |   Andrew  |    15    |     0     |
    |    2    |    John   |    12    |     0     |
    |    3    |    Ian    |    10    |     1     | <--should be 0.
.
.
.
    |    4    |    Pia    |     0    |     1     | <---newly inserted(sample)

and then I will call a row in the table where col stat = '1'

public List<Pdeasyupdate> getScoreEasy() {
    List<Pdeasyupdate> pdu = new ArrayList<Pdeasyupdate>();

    SQLiteDatabase db = this.getWritableDatabase();
    String selectQuery = "SELECT  * FROM " + TABLE_NAME_PLAYER + " WHERE "+
            COLUMN_NAME_STAT +" = '1'";

    Cursor c = db.rawQuery(selectQuery, null);

    if (c.moveToFirst()) {
        do {
            Pdeasyupdate ed = new Pdeasyupdate();
            ed.setePname(c.getString(1));
            ed.seteEscr(Integer.parseInt(c.getString(2)));
            ed.seteHscr(Integer.parseInt(c.getString(3)));
            ed.seteDscr(Integer.parseInt(c.getString(4)));
            // Adding contact to list
            pdu.add(ed);
        } while (c.moveToNext());
        db.close();
    }

and so, expect that the newly inserted row will be called because the other row has already stat = 0 .

the BAD news is; it returns null in all

    Integer.parseInt(c.getString()));

You can do exec

db.execSQL("UPDATE my_table SET stat = 0 ");

Also if you still didnt see any changes, probably there is problem with transactions, so try this

db.beginTransaction();
try {
    db.execSQL("UPDATE my_table SET stat = 0 ");
    db.setTransactionSuccessful();
} catch {
    //Error in between database transaction 
} finally {
    db.endTransaction();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM