Chinaunix首页 | 论坛 | 博客
  • 博客访问: 894832
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: Java

2010-12-31 14:10:28

Android学习笔记(十二)

15 次阅读, , , , by flysolo.

事物处理

PersonService

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package org.wp.service;  
 
/** 
 *  
 * 使用SQLiteDatabase的beginTransaction()方法可以开启一个事务 
 * 程序执行到endTransaction() 方法时会检查事务的标志是否为成功 
 * 如果为成功则提交事务,否则回滚事务 
 * 当应用需要提交事务,必须在程序执行到endTransaction()方法之前 
 * 使用setTransactionSuccessful() 方法设置事务的标志为成功 
 * 如果不调用setTransactionSuccessful() 方法,默认会回滚事务 
 *  
 */  
 
import java.util.ArrayList;  
import java.util.List;  
import org.wp.domain.Person;  
import android.content.Context;  
import android.database.Cursor;  
import android.database.sqlite.SQLiteDatabase;  
import android.util.Log;  
 
public class PersonService {  
 
    private static final String TAG = "PersonService";  
 
    private DataBaseOpenHelper dataBaseOpenHelper;  
    private SQLiteDatabase database;  
 
    public PersonService(Context context) {  
        dataBaseOpenHelper = new DataBaseOpenHelper(context);  
    }  
 
    public void save() {  
        database = dataBaseOpenHelper.getWritableDatabase();  
        // 开始事务  
        database.beginTransaction();  
        try {  
            database.execSQL("insert into person(name,age) values(?,?)",  
                    new Object[] { "张三", (short) 26 });  
            database.execSQL("insert into person(name,age) values(?,?)",  
                    new Object[] { "田七", (short) 35 });  
            // 调用此方法会在执行到endTransaction() 时提交当前事务,如果不调用此方法会回滚事务  
            database.setTransactionSuccessful();  
        } catch (Exception e) {  
            Log.i(TAG, e.toString());  
        }  
        // 由事务的标志决定是提交事务,还是回滚事务  
        database.endTransaction();  
    }  
 
    public List<Person> getScrollData(int startResult, int maxResult) {  
        List<Person> persons = new ArrayList<Person>();  
        database = dataBaseOpenHelper.getReadableDatabase();  
        Cursor cursor = database.rawQuery("select * from person limit ?,?",  
                new String[] { String.valueOf(startResult),  
                        String.valueOf(maxResult) });  
        while (cursor.moveToNext()) {  
            persons.add(new Person(cursor.getInt(0), cursor.getString(1),  
                    cursor.getShort(2)));  
        }  
        return persons;  
    }  
 
}

PersonServiceTest

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package org.wp.db;  
 
import java.util.List;  
import org.wp.domain.Person;  
import org.wp.service.PersonService;  
import android.test.AndroidTestCase;  
import android.util.Log;  
 
public class PersonServiceTest extends AndroidTestCase {  
    private static final String TAG = "PersonServiceTest";  
 
    public void testSave() {  
        PersonService personService = new PersonService(this.getContext());  
        personService.save();  
    }  
 
    public void testGetScrollData() throws Exception {  
        PersonService personService = new PersonService(this.getContext());  
        List<Person> persons = personService.getScrollData(0, 30);  
        for (Person person : persons) {  
            Log.i(TAG, person.toString());  
        }  
    }  
}
阅读(715) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~