android SQLite数据库的增删改查以及事务的单元测试
2010-11-05 00:56:30 来源:WEB开发网db.execSQL("delete fro person where personid=?", new Object[] { id });
}
public List< Person> getScrollData(int firstResult, int maxResult) {
List< Person> persons = new ArrayList< Person>();
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = db.rawQuery(
"select personid,name,age from person limit ?,?",
new String[] { String.valueOf(firstResult),
String.valueOf(maxResult) });// firstResult开始索引
while (cursor.moveToNext()) {// maxResult每页获取的记录数
Log.i("aa", "333333333333");
Person person = new Person();
person.setId(cursor.getInt(cursor.getColumnIndex("personid")));
person.setName(cursor.getString(1));
person.setAge(cursor.getInt(2));
persons.add(person);
}
cursor.close();
return persons;
}
public long getCount() {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from person", null);
// 没有占位符参数的话,直接用null
cursor.moveToFirst();
Long count = cursor.getLong(0);
cursor.close();
return count;
}
}
新建测试类FileServiceTest.java
package com.gohighsoft.db;
import android.test.AndroidTestCase;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;
public class FileServiceTest extends AndroidTestCase {
public void createDatabase() throws Throwable{
DataBaseHaler databaseHelper = new DataBaseHaler(getContext());
databaseHelper.getWritableDatabase();
更多精彩
赞助商链接