首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

Android开发之用SQLite实现增下改查操作和事务操作

2013-03-17 
Android开发之用SQLite实现增上改查操作和事务操作?1.写一个获得创建修改数据库的工具类,这个类继承自SQLi

Android开发之用SQLite实现增上改查操作和事务操作

?

1.写一个获得创建修改数据库的工具类,这个类继承自SQLiteOpenHelper

?

?

3.写业务逻辑类PersonService,其中包含了增上改查,看代码吧

??

package com.xiaobo.juit;import java.util.List;import android.test.AndroidTestCase;import android.util.Log;import com.xiaobo.domain.Person;import com.xiaobo.service.DbOpenHelper;import com.xiaobo.service.PersonService;public class PersonServiceTest extends AndroidTestCase{// 本测试类的专有TAG标签,利于LogCat调试private final static String TAG = "PersonServiceTest";/** * 测试创建或更改数据库 * @throws Throwable */public void testCreateDb() throws Throwable{DbOpenHelper dbOpenHelper = new DbOpenHelper(getContext());dbOpenHelper.getWritableDatabase();}/** * 测试保存 * @throws Throwable */public void testSave() throws Throwable{PersonService personService = new PersonService(getContext());personService.save(new Person(1, "yellow", "18706487378", 2000));personService.save(new Person(2, "yuan", "18706697820", 3000));personService.save(new Person(3, "zhen", "13658662813", 4000));}/** * 测试根据id查找一条记录 * @throws Throwable */public void testFind() throws Throwable{PersonService personService = new PersonService(getContext());Person person = personService.find(1);Log.i(TAG, person.toString());}/** * 测试更新记录 * @throws Throwable */public void testUpdate() throws Throwable{PersonService personService = new PersonService(getContext());personService.update(new Person(2, "yellow", "18706487378", 5000));personService.update(new Person(3, "yuan", "18706697820", 5000));}/** * 测试删除记录 * @throws Throwable */public void testDelete() throws Throwable{PersonService personService = new PersonService(getContext());personService.delete(1);}/** * 测试获得记录总条数 * @throws Throwable */public void testGetCount() throws Throwable{PersonService personService = new PersonService(getContext());Log.i(TAG, String.valueOf(personService.getCount()));}/** * 测试分页记录 * @throws Throwable */public void testGetScrollData() throws Throwable{PersonService personService = new PersonService(getContext());List<Person> persons = personService.getScrollData(0, 5);for(Person person :persons){Log.i(TAG, person.toString());}}/** * 测试转账汇款 * @throws Throwable */public void testUpdateAmount() throws Throwable{PersonService personService = new PersonService(getContext());Person p01 = personService.find(2);Person p02 = personService.find(3);System.out.println(p01.toString());System.out.println(p02.toString());p01.setAmount(500);p02.setAmount(600);personService.update(p01);personService.update(p02);Log.i(TAG, p01.getAmount().toString());Log.i(TAG, p02.getAmount().toString());}/** * 测试转账汇款2 * @throws Throwable */public void testPayment() throws Throwable{PersonService personService = new PersonService(getContext());Person p01 = personService.find(2);Person p02 = personService.find(3);// 打印转账之前账户信息Log.i(TAG, p01.toString());Log.i(TAG, p02.toString());personService.payment(2, 3);// 再次打印转账之后账户信息Log.i(TAG, p01.toString());Log.i(TAG, p02.toString());}}

?

5.大家可以下源代码看一下,不是很会写原理,呵呵,希望大家共同进步

? ?

?

热点排行