001 業務類
002 package com.smart.dh;
003 import
java
.util.Iterator;
004 import java.util.List;
005 import
android
.test.AndroidTestCase;
006 import
android
.util.Log;
007 import com.smart.domain.Person;
008 import com.smart.service.PersonService;
009 public class PersonServiceTest extends AndroidTestCase {
010 private static final String TAG="PersonServiceTest";
011
012 //保存數據。
013 public void testSave() throws Exception{
014 PersonService personService=new PersonService(this.getContext());
015 // personService.save(new Person("老梁",(short)23));
016 for (int i = 0; i < 10; i++) {
017 personService.save(new Person("llb"+i,(short)(i+1)));
018 }
019
020
021 }
022
023
024 //查詢
025 public void testFind() throws Exception{
026 PersonService personService=new PersonService(this.getContext());
027 Person person=personService.find(1);
028 Log.i(TAG, person.toString());
029
030 // personService.save(new Person("老梁",(short)23));
031 }
032 //更新語句
033 public void testUpdate() throws Exception{
034 PersonService personService=new PersonService(this.getContext());
035 Person person=personService.find(1);
036 person.setName("smart");
037 personService.update(person);
038
039 Log.i(TAG, person.toString());
040
041 }
042 //得到全部的條數
043 public void testGetCount() throws Exception{
044 PersonService personService=new PersonService(this.getContext());
045 Log.i(TAG, String.valueOf(personService.getCount()));
046
047 }
048
049 //分頁功能
050 public void testGetScrollData() throws Exception{
051 PersonService personService=new PersonService(this.getContext());
052 List persons=personService.getScrollData(0, 20);//從0條到20條的數據 053 for(Person person:persons){ 054 Log.i(TAG, person.toString()); 055 } 056 057 058 } 059 060 public void testDelete() throws Exception{ 061 PersonService personService=new PersonService(this.getContext()); 062 personService.delete(1,2,3);//刪除1.2.3三條記錄 063 } 064 065 066 067 068 069 070 071 } 072 073 javaBean類 074 package com.smart.domain; 075 public class Person { 076 @Override 077 public String toString() { 078 079 return "personid="+personid+",name="+name+",age="+age; 080 } 081 public int personid; 082 public String name; 083 public Short age; 084 public int getPersonid() { 085 return personid; 086 } 087 public void setPersonid(int personid) { 088 this.personid = personid; 089 } 090 public String getName() { 091 return name; 092 } 093 public void setName(String name) { 094 this.name = name; 095 } 096 // 增長一個構造器 097 public Person(int personid, String name, Short age) { 098 super(); 099 this.personid = personid; 100 this.name = name; 101 this.age = age; 102 } 103 //建立構造器 104 public Person(String name, short age) { 105 this.name = name; 106 this.age = age; 107 108 } 109 public Short getAge() { 110 return age; 111 } 112 public void setAge(Short age) { 113 this.age = age; 114 } 115 } 116 117 118 數據庫建立類 119 package com.smart.service; 120 import android.content.Context; 121 import android.database.sqlite.SQLiteDatabase; 122 import android.database.sqlite.SQLiteDatabase.CursorFactory; 123 import android.database.sqlite.SQLiteOpenHelper; 124 public class DataBaseOpenHelper extends SQLiteOpenHelper { 125 // 數據名稱, 126 private static final String DBNAME = "smrtDataBase"; 127 // 數據庫版本 128 private static final int version = 1; 129 // 構造方法參數, 130 public DataBaseOpenHelper(Context context) { 131 super(context, DBNAME, null, version); 132 } 133 // 數據庫建立表的名子。 134 @Override 135 public void onCreate(SQLiteDatabase db) { 136 db.execSQL("CREATE TABLE person (personid integer primary key autoincrement,name varchar(20),age INTEGER)"); 137 } 138 // 更新方法 139 @Override 140 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 141 db.execSQL("EROP TABLE IF EXISTS person"); 142 onCreate(db); 143 } 144 145 146 147 }