什么是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():关闭游标,释放资源

本文作者:TranquilTimber

本文链接:https://www.cnblogs.com/gbrr/p/17218827.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   喝着农药吐泡泡o  阅读(506)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
🔑