Cursor是一个接口,用来读取数据库query回来的结果集,Cursor是不同步的,需要同步数据的话要在自己的方法中实现。
/**
* 取出所有笔记
* @return
*/
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_BODY}, null, null, null, null, null);
}
1.取的数据是否为空的判断
if (!cursor.moveToFirst()) {
Log.v("NotepadActivity", "没有数据!");
}
if (cursor.getCount() == 0) {
Log.v("NotepadActivity", "没有数据!");
}
2.循环取数据
mDbHelper = new NoteDbAdapter(this);
mDbHelper.open();
//插入几条数据
mDbHelper.createNote("Note_1", "Note 1 contetn!");
mDbHelper.createNote("Note_2", "Note 2 contetn!");
mDbHelper.createNote("Note_3", "Note 3 contetn!");
Cursor cursor = mDbHelper.fetchAllNotes();
while (cursor.moveToNext()) {
int columnIndex = cursor.getColumnIndex("title");
String titleString = cursor.getString(columnIndex);
Log.v("title", titleString);
}