设为首页 加入收藏

TOP

Android数据处理---SQLite(二)
2014-11-24 14:14:40 来源: 作者: 【 】 浏览:5
Tags:Android 数据处理 ---SQLite
) {


Person person = new Person();


person.setName(cursor.getString(cursor.getColumnIndex("name")));


person.setId(cursor.getInt(0));


person.setAge(cursor.getInt(2));


cursor.close(); // 关闭游标



return person;



}



return null;



}


public void delete(int id) {


SQLiteDatabase db = dbHelper.getReadableDatabase();


String sql = "delete from person where personid= ";


db.execSQL(sql, new Object[] { id }); }


public List getScrollData(int startIdx, int count) {


SQLiteDatabase db = dbHelper.getReadableDatabase();


String sql = "select * from person limit , ";


Cursor cursor = db.rawQuery(sql,


new String[] { String.valueOf(startIdx),


String.valueOf(count) });


List list = new ArrayList();


while(cursor.moveToNext()){


Person p = new Person();


p.setId(cursor.getInt(0));


p.setName(cursor.getString(1));


p.setAge(cursor.getInt(2));


list.add(p);


}


cursor.close();


return list;


}


public long getRecordsCount() {


SQLiteDatabase db = dbHelper.getReadableDatabase();


String sql = "select count(*) from person";


Cursor cursor = db.rawQuery(sql, null);


cursor.moveToFirst();


long count = cursor.getInt(0);


cursor.close();


return count;


}


}


在测试类cn.class3g.db. PersonServiceTest中添加对应测试方法


public class PersonServiceTest extends AndroidTestCase {


public void testSave() throws Throwable{


PersonService service = new PersonService(this.getContext());


Person person = new Person();


person.setName("zhangxiaoxiao");


service.save(person);


Person person2 = new Person();


person2.setName("laobi");


service.save(person2);


Person person3 = new Person();


person3.setName("lili");


service.save(person3); Person person4 = new Person(); person4.setName("zhaoxiaogang");


service.save(person4);


}


public void testUpdate() throws Throwable{


PersonService ps = new PersonService(this.getContext());


Person person = new Person("Ton", 122);


ps.update(person, 2);//需要实现查看数据库中Ton的id值


}


public void testFind() throws Throwable{


PersonService ps = new PersonService(this.getContext());


Person person = ps.find(2);


Log.i("TAG",person.toString());


}


public void testDelete() throws Throwable{


PersonService ps = new PersonService(this.getContext());


ps.delete(2);


}


public void testScroll() throws Throwable{


PersonService service = new PersonService(this.getContext());


List personList = service.getScrollData(3, 2);


Log.i("TAG",personList.toString());


}


public void testCount() throws Throwable{


PersonService service = new PersonService(this.getContext());


long count = service.getRecordsCount();


Log.i("TAG", String.valueOf(count));


}


}


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Matlab中三维散点图的绘制 下一篇Android模拟打电话应用程序实现分..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: