Content Value和Cursor的一些认识及其方法的使用

来源:互联网 发布:mac下java命令 编辑:程序博客网 时间:2024/05/11 23:26


ContentValue用来向数据库的表中插入新的行。每一个Content Values对象都将一个表行表示为列名到值的映射。

数据库查询作为Cursor对象返回。Cursor是底层数据中的结果集的指针,它没有提取和返回结果值的副本。Cursor为控制在数据库查询的结果集中的位置(行)提供了一种易于管理的方式。

 

在理解和使用 Android Cursor的时候你必须先知道关于 Cursor 的几件事情:

Cursor是每行的集合、使用 moveToFirst()定位第一行、你必须知道每一列的名称、你必须知道每一列的数据类型、Cursor 是一个随机的数据源、所有的数据都是通过下标取得。

Android查询数据是通过Cursor 类来实现的。当我们使用 SQLiteDatabase.query()方法时,就会得到Cursor对象, Cursor所指向的就是每一条数据。Cursor位于 android.database.Cursor类,可见出它的设计是基于数据库服务产生的。

 

Cursor类包含了多个导航函数,其中包含但不限于以下几种:

moveToFirst():把游标移动到查询结果的第一行,返回boolean类型,返回false代表游标为空。

booleanandroid.database.Cursor.moveToFirst()

Movethe cursor to the first row.

Thismethod will return false if the cursor is empty.

Returns:whether the move succeeded.

 

moveToNext():把游标移动到下一行。

booleanandroid.database.Cursor.moveToNext()

Movethe cursor to the next row.

Thismethod will return false if the cursor is already past the last entry in theresult set.

Returns:whether the move succeeded.

 

moveToPrevious():把游标移动到前一行。

booleanandroid.database.Cursor.moveToPrevious()

Movethe cursor to the previous row.

Thismethod will return false if the cursor is already before the first entry in theresult set.

Returns:whether the move succeeded.

 

getCount():返回结果集中的行数。

intandroid.database.Cursor.getCount()

Returnsthe numbers of rows in the cursor.

Returns:the number of rows in the cursor.

 

getColumnIndexOrThrowString columnNamethrows IllegalArgumentException:返回具有指定名称的列的索引(如果不存在拥有该名称的列,就会抛出异常),索引从0开始计数。

intandroid.database.Cursor.getColumnIndexOrThrow(String columnName) throwsIllegalArgumentException

Returnsthe zero-based index for the given column name, or throwsIllegalArgumentException if the column doesn't exist. If you're not sure if acolumn will exist or not use getColumnIndex(String) and check for -1, which ismore efficient than catching the exceptions.

Parameters:columnName the name of the target column.

Returns:the zero-based column index for the given column name

Throws:IllegalArgumentException - if the column does not exist

SeeAlso: getColumnIndex(String)

 

getColumnNameint columnIndex):返回指定列索引的名称。

Stringandroid.database.Cursor.getColumnName(int columnIndex)

Returnsthe column name at the given zero-based column index.

Parameters:columnIndex the zero-based index of the target column.

Returns:the column name for the given column index.

 

getColumnNames():返回当前Cursor中所有列名的字符串数组。

String[]android.database.Cursor.getColumnNames()

Returnsa string array holding the names of all of the columns in the result set in theorder in which they were listed in the result.

Returns:the names of the columns returned in this query.

 

moveToPositionint position):将游标移动到指定行。

booleanandroid.database.Cursor.moveToPosition(int position)

Movethe cursor to an absolute position. The valid range of values is -1 <=position <= count.

Thismethod will return true if the request destination was reachable, otherwise, itreturns false.

Parameters:position the zero-based position to move to.

Returns:whether the requested move fully succeeded.

 

getPosition():返回当前游标位置。

intandroid.database.Cursor.getPosition()

Returnsthe current position of the cursor in the row set. The value is zero-based.When the row set is first returned the cursor will be at positon -1, which isbefore the first row. After the last row is returned another call to next()will leave the cursor past the last entry, at a position of count().

Returns:the current cursor position.

 

getBlobint columnIndex):获取存入数据库的图片(Bitmap

byte[]android.database.Cursor.getBlob(int columnIndex)

Returnsthe value of the requested column as a byte array.

Theresult and whether this method throws an exception when the column value isnull or the column type is not a blob type is implementation-defined.

Parameters:columnIndex the zero-based index of the target column.

Returns:the value of that column as a byte array.

示例:

publicBitmap getBmp(int position)

{

   SQLiteDatabase db = getReadableDatabase();

   Cursor cursor = select(TB_NAME);

   cursor.moveToPosition(position);

   byte[] in =cursor.getBlob(cursor.getColumnIndex(IMAGE));

   Bitmap bmpout =BitmapFactory.decodeByteArray(in, 0, in.length);

   return bmpout;

}

 

getStringint columnIndex):获得指定列的值,以String类型返回。

Stringandroid.database.Cursor.getString(int columnIndex)

Returnsthe value of the requested column as a String.

Theresult and whether this method throws an exception when the column value isnull or the column type is not a string type is implementation-defined.

Parameters:columnIndex the zero-based index of the target column.

Returns:the value of that column as a String.

 

isBeforeFirst():返回游标是否指向第一行之前的位置。

booleanandroid.database.Cursor.isBeforeFirst()

Returnswhether the cursor is pointing to the position before the first row.

Returns:whether the cursor is before the first result.

 

isAfterLast():返回游标是否指向第最后一行之后的位置。

booleanandroid.database.Cursor.isAfterLast()

Returnswhether the cursor is pointing to the position after the last row.

Returns:whether the cursor is after the last result.

 

isClosed():如果返回 true即表示该游戏标己关闭

booleanandroid.database.Cursor.isClosed()

returntrue if the cursor is closed

Returns:true if the cursor is closed.

 

访问 Cursor的下标获得其中的数据:

intnameColumnIndex = cur.getColumnIndex(People.NAME);

Stringname = cur.getString(nameColumnIndex);

现在让我们看看如何循环 Cursor取出我们需要的数据

while(cur.moveToNext()){

  //光标移动成功

  String email =cursor.getString(cursor.getColumnIndex(RuiXin.EMAIL));

  startManagingCursor(cursor);  //查找后关闭游标

  //把数据取出

}

cur.moveToNext()为假时将跳出循环,即 Cursor 数据循环完毕。

如果你喜欢用 for循环而不想用While 循环可以使用Google 提供的几下方法:isBeforeFirst()isAfterLast()isClosed()

有了以上的方法,可以如此取出数据:

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

 }

0 0