) {
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));
}
}