public class Person {
private int personid;
private String name;
private String phone;
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 String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Person [personid=" + personid + ", name=" + name + ", phone="
+ phone + "]";
}
}
package com.hbsi.csdn.operatedb;
import com.hbsi.csdn.service.DBHelper;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class PersonContentProvider extends ContentProvider {
private DBHelper dbhelper;
private static final int code=1;
private static final UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static{
sMatcher.addURI("com.hbsi.providers.personprovider", "person", code);
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db=dbhelper.getWritableDatabase();
switch(sMatcher.match(uri)){
case code:
long rowid=db.insert("person", "name", values);
return ContentUris.withAppendedId(uri, rowid);
default:
throw new IllegalArgumentException("unknow uri"+uri);
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
this.dbhelper=new DBHelper(this.getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
}
package com.hbsi.csdn.operatedb;
import com.hbsi.csdn.service.DBHelper;
import android.test.AndroidTestCase;
public class TestDB extends AndroidTestCase {
public void testDBCreate() throws Throwable{
DBHelper dbh=new DBHelper(this.getContext());
dbh.getWritableDatabase();//创建数据库
}
}
package com.hbsi.csdn.operatedb;
import java.util.List;
import com.hbsi.csdn.domain.Person;
import com.hbsi.csdn.service.DBHelper;
import com.hbsi.csdn.service.PersonService;
import android.test.AndroidTestCase;
import android.util.Log;
public class TestPersonService extends AndroidTestCase {
public void testInsert() throws Throwable{
PersonService ps=new PersonService(this.getContext());
Person person1=new Person();
person1.setName("tttt");
person1.setPhone("112211");
ps.Insert(person1);
Person person2=new Person();
person2.setName("yyyyyyy");
person2.setPhone("112211333");
ps.Insert(person2);
Person person3=new Person();
person3.setName("uuuuuu");
person3.setPhone("112211333");
ps.Insert(person3);
}
public void testUpdate() throws Throwable{
PersonService ps=new PersonService(this.getContext());
Person person1=new Person();
person1.setName("ee");
person1.setPhone("000000");
ps.Update(person1, 1);
}
public void testDelete() throws Throwable{
PersonService ps=new PersonService(this.getContext());
ps.Delete(1);
}
public void testFind() throws Throwable{
PersonService ps=new PersonService(this.getContext());
Person person=ps.find(2);
Log.i("TAG",person.toString());
}
public void testScrollData() throws Throwable{
PersonService ps=new PersonService(this.getContext());
List<Person> person=ps.getScrollData(2, 5);
Log.i("TAG",person.toString());
}
public void testGetCount() throws Throwable{
PersonService ps=new PersonService(this.getContext());
int a=ps.getCount();
Log.i("TAG",String.valueOf(a));
}
}