还有一件事,大家下载的时候,不要加数据库连接驱动包。本项目是不用的。
先让我们看一下图先。
业务类
package com.smart.dh;
import java.util.Iterator;
import java.util.List;
import android.test.AndroidTestCase;
import android.util.Log;
import com.smart.domain.Person;
import com.smart.service.PersonService;
public class PersonServiceTest extends AndroidTestCase {
private static final String TAG="PersonServiceTest";
//保存数据。
public void testSave() throws Exception{
PersonService personService=new PersonService(this.getContext());
// personService.save(new Person("老梁",(short)23));
for (int i = 0; i < 10; i++) {
personService.save(new Person("llb"+i,(short)(i+1)));
}
}
//查询
public void testFind() throws Exception{
PersonService personService=new PersonService(this.getContext());
Person person=personService.find(1);
Log.i(TAG, person.toString());
// personService.save(new Person("老梁",(short)23));
}
//更新语句
public void testUpdate() throws Exception{
PersonService personService=new PersonService(this.getContext());
Person person=personService.find(1);
person.setName("smart");
personService.update(person);
Log.i(TAG, person.toString());
}
//获得所有的条数
public void testGetCount() throws Exception{
PersonService personService=new PersonService(this.getContext());
Log.i(TAG, String.valueOf(personService.getCount()));
}
//分页功能
public void testGetScrollData() throws Exception{
PersonService personService=new PersonService(this.getContext());
List<Person> persons=personService.getScrollData(0, 20);//从0条到20条的数据
for(Person person:persons){
Log.i(TAG, person.toString());
}
}
public void testDelete() throws Exception{
PersonService personService=new PersonService(this.getContext());
personService.delete(1,2,3);//删除1.2.3三条记录
}
}
javaBean类
package com.smart.domain;
public class Person {
@Override
public String toString() {
return "personid="+personid+",name="+name+",age="+age;
}
public int personid;
public String name;
public Short age;
public int getPersonid() {
return personid;
}
public void setPersonid(int personid) {
this.personid = personid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 增加一个构造器
public Person(int personid, String name, Short age) {
super();
this.personid = personid;
this.name = name;
this.age = age;
}
//创建构造器
public Person(String name, short age) {
this.name = name;
this.age = age;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
}
数据库创建类
package com.smart.service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseOpenHelper extends SQLiteOpenHelper {
// 数据名称,
private static final String DBNAME = "smrtDataBase";
// 数据库版本
private static final int version = 1;
// 构造方法参数,
public DataBaseOpenHelper(Context context) {
super(context, DBNAME, null, version);
}
// 数据库创建表的名子。
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE person (personid integer primary key autoincrement,name varchar(20),age INTEGER)");
}
// 更新方法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("EROP TABLE IF EXISTS person");
onCreate(db);
}
}
转载:http://www.adobex.com/android/source/details/00000012.htm