什么是cursor?怎么使用cursor?

Cursor

在Android查询数据时就是通过Cursor类来实现的。当我们使用SQLiteDatabase.query()方法时,就会得到Cursor对象,Cursor所指向的就是每一条数据。

举例:

 while (cursor.moveToNext()) {
//光标移动成功
//把数据取出来 @SuppressLint(
"Range") String newName = cursor.getString(cursor.getColumnIndex("name")); @SuppressLint("Range") int newAge = cursor.getInt(cursor.getColumnIndex("age")); // show.setText(show.getText() + "\n" + newName + "\t" + newAge); show.setText(show.getText() + "\n" + newName); showAge.setText(showAge.getText() + "\n" + newAge); }

 

Cursor是每行的集合。

使用moveToFirst()移动光标到下一行

getColumnIndex()返回指定列的名称,如果不存在返回-1

close():关闭游标,释放资源

posted @ 2023-03-15 15:47  喝着农药吐泡泡o  阅读(434)  评论(0编辑  收藏  举报