Android 数据库SQLite Cursor数据遍历读取

public List<PointBean> getAllPoints() {
		String sql = "select * from points";
		SQLiteDatabase db = helper.getWritableDatabase();
		List<PointBean> pointList = new ArrayList<PointBean>();
		PointBean point = null;
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			point = new PointBean();
			point.setPointName(cursor.getString(cursor
					.getColumnIndex("point_name")));
			point.setLongitude(cursor.getDouble(cursor
					.getColumnIndex("longitude")));
			point.setLatitude(cursor.getDouble(cursor
					.getColumnIndex("latitude")));
			pointList.add(point);
		}
		return pointList;
	}



Cursor是SQLite 数据库查询返回的行数集合,Cursor是一个游标接口,提供了遍历查询结果的方法,如移动指针方法move(),获得列值方法getString()等。以下是Cursor遍历的方法。

工具/原料

  • 编译环境Eclipse

方法/步骤

  1. 1

    通过query获取数据:

    SQLiteDatabase dataBase=sqliteDB.openDB();

    String table="tab_running";

    String[] columns={"startaddress","startgpstime","startsystime","endaddress","endgpstime","endsystime"};//你要的数据  

    String selection="finished=?";  

    String[] selectionArgs={"1"};//具体的条件,注意要对应条件字段  

    Cursor cursor=dataBase.query(table, columns, selection, selectionArgs, null, null, null);

  2. 2

    根据列索引遍历读取列数据:

    while(cursor.moveToNext())

    {

    //根据列的索引直接读取  比如第0列的值

       String strValue= cursor.getString(0);  

    }

  3. 3

    根据列名获取列索引遍历读取列数据:

    while(cursor.moveToNext())

    {

    //根据列名获取列索引   

    int nameColumnIndex = cursor.getColumnIndex(“username");

    String strValue=cursor.getString(nameColumnIndex); 

    }

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值