简体   繁体   中英

I am trying to update score of the player in sql lite database in android but its not happening

This is the update function in DBHelper class, which updates the score by 10 after each correct answer. My insert function is working fine, but when the update function triggers app is changing its activity and score is not getting update

public Boolean updateScore(String username,Integer score)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("username", username);
    contentValues.put("score", score);
    long result = db.update( "scores",contentValues, username + " = ?", new String[] 
{username});
    if (result==-1) return false;
    else return true;
}

This is my coding behind the check button. It checks if the word is correct then the score is increased by 10. Insert function is working fine.

        check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            player.start();

            if (answer.getText().toString().isEmpty()){
                Toast.makeText(easy_level.this,"Enter you guess first", 
Toast.LENGTH_SHORT).show();
            }
            else if (answer.getText().toString().equalsIgnoreCase(pet)){
                Toast.makeText(easy_level.this,"You are correct", Toast.LENGTH_SHORT).show();

                answer.setText("");
                pet = pets[random.nextInt(pets.length)];
                question.setText(mixWords(pet));
                count.start();

                //insertingScore
                if(score_int == 0){
                    Boolean insert = DB.insertScore(name,10);
                    score_int +=10;
                    score.setText(String.valueOf(score_int));
                    if (insert==true)
                    {
                        Toast.makeText(easy_level.this,"added 10",Toast.LENGTH_SHORT).show();

                    }else
                    {
                        Toast.makeText(easy_level.this,"failed",Toast.LENGTH_SHORT).show();
                    }

                }else {
                        score_int +=10;
                        score.setText(String.valueOf(score_int));
                        Boolean update = DB.updateScore(name,score_int);

                        if (update==true)
                        {
                            Toast.makeText(easy_level.this,"updated 
10",Toast.LENGTH_SHORT).show();

                        }else
                        {
                            Toast.makeText(easy_level.this,"update 
fail",Toast.LENGTH_SHORT).show();
                        }
                }



            }else {
                Toast.makeText(easy_level.this,"You are wrong", Toast.LENGTH_SHORT).show();
            }
        }
    });

In your db.update() you are referencing the value instead of the column name

 long result = db.update( "scores",contentValues, username + " = ?", new String[] 
{username});

should be

 long result = db.update( "scores",contentValues, "username = ?", new String[] 
{username});

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