0

I'm trying to reset my database to the default state after pressing restartButton. My problem is after pressing restartButton it seems I cannot retrieve the newly added data. What am I doing wrong here?

restartButton code:

private void easySQLRestart() {
        sql = new SQLDatabaseLevelOne(MainActivity.this);
        sql.open();
        sql.restart();
        sql.close();
    }

code for restart:

public void restart() {
        sqlDatabase = dbHelper.getWritableDatabase();
        sqlDatabase.delete(DATABASE_TABLE, null, null);
    }

This is the part where I add new data after reset:

public void updateScore(int score) {
                String strScore;

                if (score == 0) {
                    score = 1;
                } else if (score > 0) {
                    strScore = getScore();

                    if (strScore.equals("")) {
                        score = 1;
                    } else {
                        strScore = getScore();
                        int value = Integer.parseInt(strScore);
                        score += value;
                    }
                }

                ContentValues dbContentValues = new ContentValues();
                dbContentValues.put(KEY_SCORE, score);
                sqlDatabase.update(DATABASE_TABLE, dbContentValues, KEY_ID + "=" + 1,
                        null);
            }

This the code for retrieving the data:

    public String getScore() {
        String[] data = new String[] { KEY_ID, KEY_SCORE };
        Cursor dbCursor = sqlDatabase.query(DATABASE_TABLE, data, null, null,
                null, null, null);
        String dbResult = "";

        int dbQuestionStatus = dbCursor.getColumnIndex(KEY_SCORE);

        for (dbCursor.moveToFirst(); !dbCursor.isAfterLast(); dbCursor
                .moveToNext()) {
            dbResult += dbCursor.getString(dbQuestionStatus);
        }

        return dbResult;
    }

This is how I add data. It doesn't throw an exception:

sql = new SQLDatabaseLevelOne(
                                                EasyRoundActivity.this);
sql.open();
try {
                                            sql.updateScore(0);
                                            }catch(Exception e) {
                                                Context context = getApplicationContext();
                                                int duration = Toast.LENGTH_SHORT;
                                                Toast.makeText(context, "ERROR", duration).show();
                                        }
droidH
  • 39
  • 1
  • 2
  • 10

1 Answers1

0

SQLiteDatabase.update() updates existing records. To insert some new ones you need to call SQLiteDatabase.inset()

alex
  • 6,359
  • 1
  • 23
  • 21