简体   繁体   中英

Android SQLite incrementing a column value

I'm trying to create a score database that increments the players 'score' by one when they win by calling updateScore(). The primary key and player number are identical (I may need to restructure the DB at some point) and the final column is 'score'.

Below is the code that initially sets the score (this works), the method that gets the score (also works fine) and the method that updates the score, incrementing the relevant players score by 1. This is the part the doesn't work, is there something I should be doing differently here? Thanks.

     /** Add a record to the database of two player scores
     * @param playerId
     * @param playerScore
     **/
    public void addScore (int playerId, int playerScore) {

        SQLiteDatabase database = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(ID, playerId);
        values.put(PLAYERNUM, playerId);
        values.put(SCORE, playerScore);

        database.insert(TABLE_2PSCORES, null, values);

        database.close();

    }


    // Get the score 
    public int getScore (int playerId) { 
        SQLiteDatabase database = this.getReadableDatabase();

        Cursor cursor = database.query(TABLE_2PSCORES, COLUMNS, " player = ?", new String[] {String.valueOf(playerId) }, null, null, null, null); //null = groupby, having, orderby, limit

        if (cursor !=null) { cursor.moveToFirst(); }

        int output = cursor.getInt(2);

        return output;
    }


    // Increment score by 1
    public void updateScore (int playerId) {

        SQLiteDatabase database = this.getWritableDatabase();

        int playerScore = getScore(playerId);
        int playerScoreInc = playerScore ++;

        ContentValues values = new ContentValues();
        values.put("score", playerScoreInc);

        database.update(TABLE_2PSCORES, values, PLAYERNUM+" = ?", new String[] {String.valueOf(playerId)} );

        database.close();

    }
int playerScoreInc = playerScore ++;

This assigns playerScore to playerScoreInc and only after that increments playerScore . To first increment and then assign, change to ++playerScore .

However, you can do it all in SQL, no need to fetch score, increment it in code and then update the database table separately:

database.execSQL("UPDATE " + TABLE_2PSCORES + " SET " + SCORE + "=" + SCORE + "+1" + " WHERE " + PLAYERNUM + "=?",
    new String[] { String.valueOf(playerId) } );

The other answers solve the original question, but the syntax makes it hard to understand. This is a more general answer for future viewers.

How to increment a SQLite column value

SQLite

The general SQLite syntax is

UPDATE {Table} SET {Column} = {Column} + {Value} WHERE {Condition}

An example of this is

UPDATE Products SET Price = Price + 1 WHERE ProductID = 50

(Credits to this answer )

Android

Now that the general syntax is clear, let me translate that into Android syntax.

private static final String PRODUCTS_TABLE = "Products";
private static final String ID = "ProductID";
private static final String PRICE = "Price";

String valueToIncrementBy = "1";
String productId = "50";
String[] bindingArgs = new String[]{ valueToIncrementBy, productId };

SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("UPDATE " + PRODUCTS_TABLE +
        " SET " + PRICE + " = " + PRICE + " + ?" +
        " WHERE " + ID + " = ?",
        bindingArgs);
db.close();

TODO

This answer should be updated to use update rather than execSQL . See comment below.

Change

int playerScoreInc = playerScore ++;

to

int playerScoreInc = ++ playerScore;

I think this will work

 // Increment score by 1
    public void updateScore (int playerId) {

        SQLiteDatabase database = this.getWritableDatabase();

        int playerScore = getScore(playerId);
        int playerScoreInc = ++ playerScore;

        ContentValues values = new ContentValues();
        values.put("score", playerScoreInc);

        database.update(TABLE_2PSCORES, values, PLAYERNUM+" = ?", new String[] {String.valueOf(playerId)} );

        database.close();

    }

Have you tried debugging? Try debugging this line:

int playerScoreInc = playerScore ++;

The playerScoreInc doesn't increment.

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