android SQLite数据库的增删改查以及事务的单元测试

XPY 2010-10-29

废话少说,为了这个数据库整理一两天的时间,下面就把代码直接贴出来:

创建androidproject名称:db

创建包名:com.gohighsoft.db

新建一个Person实体类:

packagecom.gohighsoft.db;

publicclassPerson{

privateIntegerid;

privateStringname;

privateIntegerage;

publicPerson(){

};

publicPerson(Stringname,Integerage){

this.name=name;

this.age=age;

}

publicIntegergetId(){

returnid;

}

publicvoidsetId(Integerid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicIntegergetAge(){

returnage;

}

publicvoidsetAge(Integerage){

this.age=age;

}

publicStringtoString(){

return"Person[age="+age+",id="+id+",name"+name+"]";

}

}

新建工具类:

DataBaseHaler.java

packagecom.gohighsoft.db;

importandroid.content.Context;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.database.sqlite.SQLiteDatabase.CursorFactory;

importandroid.database.sqlite.SQLiteOpenHelper;

importandroid.util.Log;

publicclassDataBaseHalerextendsSQLiteOpenHelper{

privatestaticfinalStringname="sharp.db";//。db可有可无

privatestaticfinalintversion=1;//版本号不能为0

publicDataBaseHaler(Contextcontext){

super(context,NAME,null,version);

}

@Override

publicvoidonCreate(SQLiteDatabasedb){

Log.i("aa","1111111111111");

db.execSQL("CREATETABLEperson(personidintegerprimarykeyautoincrement,namevarchar(20),ageinteger)");

}

@Override

publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVewsion){

Log.i("aa","22222222222222");

db.execSQL("DROPTABLEIFEXISTSPERSON");

onCreate(db);

}

}

新建一个service服务类:

PersonService.java

packagecom.gohighsoft.db;

importjava.util.ArrayList;

importjava.util.List;

importandroid.content.Context;

importandroid.database.Cursor;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.util.Log;

publicclassPersonService{

privateDataBaseHalerdatabaseHelper;

privateContextcontext;

publicPersonService(Contextcontext){

this.context=context;

databaseHelper=newDataBaseHaler(context);

}

publicvoidsavesTrannces(Personperson){

Log.i("aa","4444444444444");

SQLiteDatabasedb=databaseHelper.getWritableDatabase();

db.beginTransaction();//开始事务

try{

for(inti=0;i<100;i++){

db.execSQL("insertintoperson(name,age)values(?,?)",

newObject[]{person.getName(),person.getAge()});

}

db.execSQL("insertintopersonww(namedd,age)values(?,?)",

newObject[]{person.getName(),person.getAge()});

db.setTransactionSuccessful();

}catch(Exceptione){

//db.endTransaction();//事务结束;

Log.i("aa","wwwwwwwwwwwwwwwwwwwwwwwww");

}

db.endTransaction();//事务结束;

Log.i("aa","55555555555555555555");

}

publicvoidsave1(Personperson){

SQLiteDatabasedb=databaseHelper.getWritableDatabase();

db.beginTransaction();//开始事务

Log.i("aa","uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu");

try{

db.setTransactionSuccessful();

db.execSQL("insertintoperson(name,age)values('Tom',21)");

}catch(Exceptione){

Log.i("aa","trttttttttttttttttttttttttttttttttttttttttttttt");

}

db.endTransaction();//事务结束;

}

publicvoidsave2(Personperson){

SQLiteDatabasedb=databaseHelper.getWritableDatabase();

db.beginTransaction();//开始事务

Log.i("aa","uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu");

try{

db.setTransactionSuccessful();

for(inti=0;i<100;i++){

db.execSQL("insertintoperson(name,age)values(?,?)",

newObject[]{person.getName(),person.getAge()});

}

}catch(Exceptione){

Log.i("aa","trttttttttttttttttttttttttttttttttttttttttttttt");

}

db.endTransaction();//事务结束;

}

publicvoidupdate(Personperson){

SQLiteDatabasedb=databaseHelper.getWritableDatabase();

db.execSQL(

"updatepersonsetname=?,age=?wherepersonid=?",

newObject[]{person.getName(),person.getAge(),

person.getId()});

}

publicPersonfind(Integerid){

SQLiteDatabasedb=databaseHelper.getReadableDatabase();

Cursorcursor=db.rawQuery(

"selectpersonid,name,agefrompersonwherepersonid=?",

newString[]{String.valueOf(id)});

if(cursor.moveToNext()){

Personperson=newPerson();

person.setId(cursor.getInt(cursor.getColumnIndex("personid")));

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

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

returnperson;

}

cursor.close();

returnnull;

}

publicvoiddelete(Integerid){

SQLiteDatabasedb=databaseHelper.getWritableDatabase();

db.execSQL("deletefropersonwherepersonid=?",newObject[]{id});

}

publicList<Person>getScrollData(intfirstResult,intmaxResult){

List<Person>persons=newArrayList<Person>();

SQLiteDatabasedb=databaseHelper.getReadableDatabase();

Cursorcursor=db.rawQuery(

"selectpersonid,name,agefrompersonlimit?,?",

newString[]{String.valueOf(firstResult),

String.valueOf(maxResult)});//firstResult开始索引

while(cursor.moveToNext()){//maxResult每页获取的记录数

Log.i("aa","333333333333");

Personperson=newPerson();

person.setId(cursor.getInt(cursor.getColumnIndex("personid")));

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

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

persons.add(person);

}

cursor.close();

returnpersons;

}

publiclonggetCount(){

SQLiteDatabasedb=databaseHelper.getReadableDatabase();

Cursorcursor=db.rawQuery("selectcount(*)fromperson",null);

//没有占位符参数的话,直接用null

cursor.moveToFirst();

Longcount=cursor.getLong(0);

cursor.close();

returncount;

}

}

新建测试类FileServiceTest.java

packagecom.gohighsoft.db;

importandroid.test.AndroidTestCase;

importjava.io.InputStream;

importjava.io.OutputStream;

importjava.util.List;

importandroid.content.Context;

importandroid.test.AndroidTestCase;

importandroid.util.Log;

publicclassFileServiceTestextendsAndroidTestCase{

publicvoidcreateDatabase()throwsThrowable{

DataBaseHalerdatabaseHelper=newDataBaseHaler(getContext());

databaseHelper.getWritableDatabase();

}

publicvoidtestSave()throwsThrowable{

PersonServicepersonservice=newPersonService(this.getContext());

Personperson=newPerson("张大千",29);

personservice.save2(person);

}

publicvoidtesetUpdate()throwsThrowable{

PersonServicepersonService=newPersonService(this.getContext());

Personperson=personService.find(1);

person.setName("jim");

personService.update(person);

}

publicvoidtestCount()throwsThrowable{

PersonServicepersonService=newPersonService(this.getContext());

Log.i("aa",personService.getCount()+"");

}

publicvoidtestGetScrollData()throwsThrowable{

PersonServicepersonService=newPersonService(this.getContext());

List<Person>persons=personService.getScrollData(0,3);

for(Personperson:persons){

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

}

}

publicvoidTrancesTest()throwsThrowable{

PersonServicepersonService=newPersonService(this.getContext());

Personperson=newPerson("aaaaaaa",28);

personService.savesTrannces(person);

}

}

AndroidManifest.xml配置文件中添加单元测试的权限:

<?xmlversion="1.0"encoding="utf-8"?>

<manifestxmlns:android="http://schemas.android.com/apk/res/android"

package="com.gohighsoft.db"android:versionCode="1"

android:versionname="1.0">

<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">

<uses-libraryandroid:name="android.test.runner"/>

<activityandroid:name=".MainActivity"android:label="@string/app_name">

<intent-filter>

<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

</application>

<uses-sdkandroid:minSdkVersion="8"/>

<instrumentationandroid:name="android.test.InstrumentationTestRunner"

android:targetPackage="com.gohighsoft.db"android:label="TestsforMyApp"/>

</manifest>

好了,至此为止,在上面的单元测试怎删改查算是弄完了,通过运行测试类的方法就可以看到效果以及具体的方法用法了。不容易呵呵不懂得方法自己到晚上去查吧,为了上面的代码我忙着了两天,不想写注视了,不理解的自己去网站找吧,反正上面的代码都是我跑起来的

相关推荐

我的剑就是你的剑 / 0评论 2020-02-01