Android 数据库SQLite Cursor数据遍历读取
2015-07-13 17:20
447 查看
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
通过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
根据列索引遍历读取列数据:
while(cursor.moveToNext())
{
//根据列的索引直接读取 比如第0列的值
String strValue= cursor.getString(0);
}
3
根据列名获取列索引遍历读取列数据:
while(cursor.moveToNext())
{
//根据列名获取列索引
int nameColumnIndex = cursor.getColumnIndex(“username");
String strValue=cursor.getString(nameColumnIndex);
}
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通过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
根据列索引遍历读取列数据:
while(cursor.moveToNext())
{
//根据列的索引直接读取 比如第0列的值
String strValue= cursor.getString(0);
}
3
根据列名获取列索引遍历读取列数据:
while(cursor.moveToNext())
{
//根据列名获取列索引
int nameColumnIndex = cursor.getColumnIndex(“username");
String strValue=cursor.getString(nameColumnIndex);
}
相关文章推荐
- android悬浮窗应用
- 使用WIFI连接android进行调试和adb操作
- android各种资源的详细解释
- 手电筒的制作
- Androidの自定义对话框AlertDialog(一)
- android线程销毁
- android知识杂记(二)
- Android Fragment 真正的完全解析
- Android Fragment 真正的完全解析
- 基于Jenkins+git+gradle的android持续集成
- Tablayout(Android) 的几个重要属性
- android studio中出现的问题
- Android EditText输入最大值提示功能
- android知识杂记(一)
- 【Android】设置全屏和横屏
- android studio配置AndroidAnnotations
- android新组件RecyclerView使用介绍和进阶使用
- android raw与assets区别
- Android开发之如何监听让服务不被杀死(service+broadcast)
- Android开发Button控件的圆角,边框