检查数据是否是在SQLite数据库中编写的

时间:2015-05-30 14:27:56

标签: java android sqlite

我想在SQLite数据库中保存测验中的分数,并在分数为5时更改另一项活动中的图像。没有显示错误,但即使我得分为5,图像也不会改变...如何记录我的数据库内容以检查分数是否已添加或如何找到错误?

DB助手:

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;
}

完成测验后将分数写入数据库:

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);
    }
});
}

发现课程 - >检查得分是否为5并在以下情况下更改图像:

 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();
}

1 个答案:

答案 0 :(得分:0)

SQLiteDatabase插入函数返回一个long值,因此如果发生错误,则返回-1。

'新插入行的行ID,如果发生错误,则为-1 http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

这可用于查看插入是否正确发生。

或者你可以包装试试并捕捉和打印这样的信息

try { 
  //code
} catch(SQLiteException ex) {

        Log.v("Insert into database exception caught", ex.getMessage());
        return -1; 
   }
}

当我在使用Java和SQLite时遇到问题时,通常使用Shell直接使用SQLite桌面版本,因为我发现测试表设计更容易。

希望这有帮助