按下restartButton后,我正在尝试将我的数据库重置为默认状态。我的问题是按下restartButton 后似乎无法检索新添加的数据。我在这里做错了什么?
重启按钮代码:
private void easySQLRestart() {
sql = new SQLDatabaseLevelOne(MainActivity.this);
sql.open();
sql.restart();
sql.close();
}
重启代码:
public void restart() {
sqlDatabase = dbHelper.getWritableDatabase();
sqlDatabase.delete(DATABASE_TABLE, null, null);
}
这是我在重置后添加新数据的部分:
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);
}
这是检索数据的代码:
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;
}
这就是我添加数据的方式。它不会抛出异常:
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();
}