/**
* 增删改查
* @author ZLQ
*
*/
public interface SQLOperate {
public void add(Person p);
public void delete(int id);
public void updata(Person p);
public List<Person> find();
public Person findById(int id);
}
/**
* 助手类
* @author ZLQ
*
*/
public class DBOpneHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;//版本
private static final String DB_NAME = "people.db";//数据库名
public static final String STUDENT_TABLE = "student";//表名
public static final String _ID = "_id";//表中的列名
public static final String NAME = "name";//表中的列名
//创建数据库语句,STUDENT_TABLE,_ID ,NAME的前后都要加空格
private static final String CREATE_TABLE = "create table " + STUDENT_TABLE + " ( " + _ID + " Integer primary key autoincrement," + NAME + " text)";
public DBOpneHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
//数据库第一次被创建时调用
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
//版本升级时被调用
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
public class Test extends AndroidTestCase {
public void testAdd() throws Exception{
SQLOperateImpl test = new SQLOperateImpl(getContext());
Person person = new Person(2, "Peter");
test.add(person);
}
public void testDelete() throws Exception{
SQLOperateImpl test = new SQLOperateImpl(getContext());
test.delete(1);
}
public void testUpdata() throws Exception{
SQLOperateImpl test = new SQLOperateImpl(getContext());
Person person = new Person(1, "Tom");
test.updata(person);
} www.iyunv.com
public void testFind() throws Exception{
SQLOperateImpl test = new SQLOperateImpl(getContext());
List<Person> persons = test.find();
for (Person person : persons) {
Log.i("System.out", person.toString());
}
}
public void testFindById() throws Exception{
SQLOperateImpl test = new SQLOperateImpl(getContext());
Person person = test.findById(2);
Log.i("System.out", person.toString());
}
}