samedi 30 mai 2015

Check if data was written in SQLite Database

I want to save the Score from a Quiz in a SQLite Database and change an image in another activity if the Score is 5. There is no error shown, but even if I score 5 the image won't change... How can I log the content of my database to check if the score was added or how can I find the mistake?

DB Helper:

public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 7;
private static final String DATABASE_NAME = "CE";

public static final String SCORE_TABLE = "score";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_SCORE = "SCORE";
public static final String COLUMN_MARKERID = "MARKERID";

 private SQLiteDatabase dbase;

 public DbHelper(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);

 }
@Override
public void onCreate(SQLiteDatabase db) {

dbase= db;

String create_query = "CREATE TABLE IF NOT EXITS " + SCORE_TABLE + " ( "
        + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COLUMN_SCORE + " INTEGER, "
        + COLUMN_MARKERID + " TEXT) ";
db.execSQL(create_query);
}

public void addScore (DbHelper dbh, Integer score, String markerID) {
    dbase = dbh.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_SCORE, score);
    cv.put(COLUMN_MARKERID, markerID);
    dbase.insert(SCORE_TABLE, null, cv);
}

public Cursor getScore(DbHelper dbh) {
    dbase = dbh.getReadableDatabase();
    String columns[] = {COLUMN_SCORE, COLUMN_MARKERID};
    Cursor cursor = dbase.query(SCORE_TABLE, columns, null, null, null, null, null);
    return cursor;
}

Write the Score into the Database after completing the Quiz:

public class ResultActivity extends Activity {

String markerID;
int score;
TextView t=(TextView)findViewById(R.id.textResult);
Button saveButton = (Button) findViewById(R.id.saveButton);
Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_layout);

Bundle b = getIntent().getExtras();
score = b.getInt("score");
markerID = b.getString("markerID");
}

saveButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DbHelper dbh = new DbHelper(context);
        dbh.addScore(dbh,score,markerID);
        Intent intent = new Intent(ResultActivity.this, Discover.class);
            intent.putExtra("MarkerID", markerID);
            startActivity(intent);
    }
});
}

Discover class -> Check if score is 5 and change image if:

 DbHelper dbh  = new DbHelper(context);
    Cursor cursor = dbh.getScore(dbh);
    cursor.moveToFirst();
    if (cursor.moveToFirst()) {
        do {
            if (Integer.parseInt(cursor.getString(0))== 5 && InfoUeberschrift.toString().equals(cursor.getString(1))){
                ImageDone.setImageResource(R.drawable.markerdone);
            }
        }
        while(cursor.moveToNext());
    }
    cursor.close();
}

Aucun commentaire:

Enregistrer un commentaire