Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite incrementing a column value

Tags:

android

sqlite

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

    }
like image 914
davidbain Avatar asked Feb 26 '14 09:02

davidbain


People also ask

Does SQLite support blob type?

SQLite only has four primitive data types: INTEGER, REAL, TEXT, and BLOB. APIs that return database values as an object will only ever return one of these four types.

How to get table SCHEMA in SQLite?

If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.

When can you get SQLite SCHEMA error?

SQLite_Schema is an error code that is returned when there is a change in the database. The SQLite _schema error code is indicated when a prepared SQL statement is no longer valid and cannot be executed. These types of errors occur when using sqlite3 prepare () and sqlite3 step () interfaces to run SQL.


1 Answers

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) } );
like image 57
laalto Avatar answered Oct 13 '22 01:10

laalto