从上一篇可知,通过sqlite数据库对象进行查询返回的都是Cursor对象,那么现在就详细分析一下Cursor类的相关内容。
Cursor简介
Cursor翻译过来是游标或指针,那么可以理解,通过sqliteDatabase对象返回的Cursor是一个指向返回结果的指针或者游标,通过这个游标提供的方法可以找到对应的数据集并进行操作。事实证明这样理解是可以的,因为Cursor是一个接口,这个接口包含很多可以对结果集进行操作的方法,如果熟悉JDBC的ResultSet,Cursor就相当于ResultSet,并且比ResultSet的操作方式更加丰富。官方文档的简介是:Cursor接口提供了一种对数据库返回结果集进行随机读写的途径, Cursor的实现类不要求是同步的,所以在多线程中使用Cursor应该只进行其各自的同步。
Cursor操作结果集的常用操作方法
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
|
||||||||||
|
|||||||||||
|
|
||||||||||
|
|
||||||||||
|
|||||||||||
|
|
||||||||||
String[] |
|
||||||||||
|
|||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
||||||||||
|
|||||||||||
|
|
||||||||||
|
|||||||||||
|
|
||||||||||
|
|||||||||||
|
|||||||||||
|
|
||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||
|
|||||||||||
Register an observer that is called when changes happen to the content backing this cursor.
|
|||||||||||
Register an observer that is called when changes happen to the contents of the this cursors data set,for example,when the data set is changed via
requery() ,deactivate() ,or
close() .
|
|||||||||||
This is an out-of-band way for the the user of a cursor to communicate with the cursor.
|
|||||||||||
Register to watch a content URI for changes.
|
|||||||||||
Unregister an observer that has prevIoUsly been registered with this cursor via
registerContentObserver(ContentObserver) .
|
|||||||||||
Unregister an observer that has prevIoUsly been registered with this cursor via
registerContentObserver(ContentObserver) .
|
使用实例
- publicList<Person>query(){
- ArrayList<Person>persons=newArrayList<Person>();
- Cursorc=queryTheCursor();
- while(c.moveToNext()){
- Personperson=newPerson();
- person._id=c.getInt(c.getColumnIndex("_id"));
- person.name=c.getString(c.getColumnIndex("name"));
- person.age=c.getInt(c.getColumnIndex("age"));
- person.info=c.getString(c.getColumnIndex("info"));
- persons.add(person);
- }
- c.close();
- returnpersons;
- }