Android SQLite increment column value

I am trying to create a score database that increases the score of players by one when they win by calling updateScore (). The primary key and the player number are identical (I may need to restructure the database at some point), and the last column is β€œscore”.

Below is the code that initially sets the score (this works), the method that gets the score (also works great), and the method that updates the score, increasing the number of relevant players by 1. This is the part that I'm working on, is there anything what should i do differently here? Thank you

/** 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(); } 
+7
android sqlite
source share
5 answers

Edit

 int playerScoreInc = playerScore ++; 

to

 int playerScoreInc = ++ playerScore; 
+3
source share
 int playerScoreInc = playerScore ++; 

This assigns playerScore to playerScoreInc and only then increases playerScore . For the first increment and subsequent assignment, change to ++playerScore .

However, you can do all this in SQL, there is no need to retrieve the score, increase it in the code, and then update the database table separately:

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

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

How to increase SQLite column value

Sqlite

Generic SQLite Syntax

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

An example of this is

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

(Credits for this answer )

Android

Now that the general syntax is clear, let me translate this 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 must be updated in order to use update , not execSQL . See Comment below.

+5
source share

I think it 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(); } 
+2
source share

Have you tried debugging? Try debugging this line:

 int playerScoreInc = playerScore ++; 

PlayerScoreInc does not increase.

0
source share

All Articles