使用過 SQLite數(shù)據(jù)庫的童鞋對 Cursor 應(yīng)該不陌生煤痕,加深自己和大家對Android 中使用 Cursor 的理解泽西。
? ? ? 關(guān)于 Cursor
? ? ? 在你理解和使用 Android Cursor 的時候你必須先知道關(guān)于 Cursor 的幾件事情:
Cursor 是每行的集合蟀架。使用 moveToFirst() 定位第一行。你必須知道每一列的名稱驮审。你必須知道每一列的數(shù)據(jù)類型译株。Cursor 是一個隨機的數(shù)據(jù)源。所有的數(shù)據(jù)都是通過下標(biāo)取得屿脐。
關(guān)于 Cursor 的重要方法:
c.move(int offset); //以當(dāng)前位置為參考,移動到指定行
c.moveToFirst();? ? //移動到第一行?
c.moveToLast();? ? //移動到最后一行?
c.moveToPosition(int position); //移動到指定行?
c.moveToPrevious(); //移動到前一行?
c.moveToNext();? ? //移動到下一行?
c.isFirst();? ? ? ? //是否指向第一條?
c.isLast();? ? //是否指向最后一條?
c.isBeforeFirst();? //是否指向第一條之前?
c.isAfterLast();? ? //是否指向最后一條之后?
c.isNull(int columnIndex);? //指定列是否為空(列基數(shù)為0)?
c.isClosed();? ? ? //游標(biāo)是否已關(guān)閉?
c.getCount();? ? ? //總數(shù)據(jù)項數(shù)?
c.getPosition();? ? //返回當(dāng)前游標(biāo)所指向的行數(shù)?
c.getColumnIndex(String columnName);//返回某列名對應(yīng)的列索引值涕蚤,如果不存在返回-1?
c.getString(int columnIndex);? //返回當(dāng)前行指定列的值?
c·getColumnIndexOrThrow(String columnName)——從零開始返回指定列名稱,如果不存在將拋出IllegalArgumentException 異常的诵。
c.close()——關(guān)閉游標(biāo)万栅,釋放資源
下面來看看一段代碼:
Button query=(Button) findViewById(R.id.query_data);
? ? ? ? query.setOnClickListener(new OnClickListener(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? SQLiteDatabase db=dbHelper.getWritableDatabase();
? ? ? ? ? ? ? ? //查詢Book表中的所有的數(shù)據(jù)
? ? ? ? ? ? ? ? //Cursor cursor=db.query("Book", null, null, null, null, null, null);
? ? ? ? ? ? ? ? Cursor cursor=db.rawQuery("Select * from Book", null);
? ? ? ? ? ? ? ? if(cursor.moveToFirst()){
? ? ? ? ? ? ? ? ? ? do{
? ? ? ? ? ? ? ? ? ? ? ? //遍歷Cursor對象,取出數(shù)據(jù)并打印
? ? ? ? ? ? ? ? ? ? ? ? String name=cursor.getString(cursor.getColumnIndex("name"));
? ? ? ? ? ? ? ? ? ? ? ? String author=cursor.getString(cursor.getColumnIndex("author"));
? ? ? ? ? ? ? ? ? ? ? ? int pages=cursor.getInt(cursor.getColumnIndex("pages"));
? ? ? ? ? ? ? ? ? ? ? ? double price=cursor.getDouble(cursor.getColumnIndex("price"));
? ? ? ? ? ? ? ? ? ? ? ? Log.d("MainActivity","book name is"+name);
? ? ? ? ? ? ? ? ? ? ? ? Log.d("MainActivity","book author is"+author);
? ? ? ? ? ? ? ? ? ? ? ? Log.d("MainActivity","book pages is"+pages);
? ? ? ? ? ? ? ? ? ? ? ? Log.d("MainActivity","book price is"+price);
? ? ? ? ? ? ? ? ? ? }while(cursor.moveToNext());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? cursor.close();
? ? ? ? ? ? }
? ? ? ? });
訪問 Cursor 的下標(biāo)獲得其中的數(shù)據(jù)
int nameColumnIndex = cur.getColumnIndex(People.NAME);
String name = cur.getString(nameColumnIndex);
現(xiàn)在讓我們看看如何循環(huán) Cursor 取出我們需要的數(shù)據(jù)
while(cur.moveToNext()) {
//光標(biāo)移動成功
String email =cursor.getString(cursor.getColumnIndex(RuiXin.EMAIL));
startManagingCursor(cursor);//查找后關(guān)閉游標(biāo)?
//把數(shù)據(jù)取出
}
當(dāng)cur.moveToNext() 為假時將跳出循環(huán)西疤,即 Cursor 數(shù)據(jù)循環(huán)完畢烦粒。
? ? ?如果你喜歡用 for 循環(huán)而不想用While 循環(huán)可以使用Google 提供的幾下方法:
·isBeforeFirst()——返回游標(biāo)是否指向之前第一行的位置
·isAfterLast()——返回游標(biāo)是否指向第最后一行的位置
·isClosed()——如果返回 true 即表示該游戲標(biāo)己關(guān)閉
有了以上的方法,可以如此取出數(shù)據(jù)
for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext())
{
int nameColumn = cur.getColumnIndex(People.NAME);
int phoneColumn = cur.getColumnIndex(People.NUMBER);
String name = cur.getString(nameColumn);
String phoneNumber = cur.getString(phoneColumn);
}
Tip:在Android 查詢數(shù)據(jù)是通過Cursor 類來實現(xiàn)的代赁。當(dāng)我們使用 SQLiteDatabase.query()方法時扰她,就會得到Cursor對象, Cursor所指向的就是每一條數(shù)據(jù)芭碍。
Cursor 位于 android.database.Cursor類徒役,可見出它的設(shè)計是基于數(shù)據(jù)庫服務(wù)產(chǎn)生的。
以上轉(zhuǎn)自:http://blog.sina.com.cn/s/blog_618199e60101fskp.html
另:Activity.startManagingCursor方法:
將獲得的Cursor對象交與Activity管理窖壕,這樣Cursor對象的生命周期便能與當(dāng)前的Activity自動同步忧勿,省去了自己對Cursor的管理。
1.這個方法使用的前提是:游標(biāo)結(jié)果集里有很多的數(shù)據(jù)記錄瞻讽。
所以鸳吸,在使用之前,先對Cursor是否為null進行判斷速勇,如果Cursor != null晌砾,再使用此方法
2.如果使用這個方法,最后也要用stopManagingCursor()來把它停止掉快集,以免出現(xiàn)錯誤贡羔。
3.使用這個方法的目的是把獲取的Cursor對象交給Activity管理廉白,這樣Cursor的生命周期便能和Activity自動同步,省去自己手動管理乖寒。