Android SQLite数据库存储实现

SQLite数据库存储数据的时候,首先必须继承SQLiteOpenHelper类:
1.    public class DbOpenHelper extends SQLiteOpenHelper { 
2.     
3.            public DbOpenHelper(Context context) { 
4.            /*
5.             * 一般是当DbOpenHelper类调用getReadableDatabase()
6.             * 或者getWritableDatabase()方法的时候,会创建数据库
7.             * 此方法中的四个参数分别代表:
8.             * context: 上下文对象
9.             * name: 数据库的名称
10.             * CursorFactory: 游标工厂,为null的时候,表示使用android系统默认的游标工厂
11.             * version: 代表版本
12.             */ 
13.            super(context, "andy.db", null, 2); 
14.        } 
15.     
16.        /*
17.         * 这个方法是当数据库创建之后,就会立即调用,
18.         * 所以一般在这个方法中,会实现创建数据库的表
19.         */ 
20.        @Override 
21.        public void onCreate(SQLiteDatabase db) { 
22.            db.execSQL("create table person(id integer primary key autoincrement, name varchar(20), age integer)"); 
23.        } 
24.     
25.        /*
26.         * 这个方法是在当数据库中的对应的数据库的版本发生变化的时候会被立即调用
27.         * 即是如果当已经存在数据库的时候,当构造方法中的第四个参数比数据库原先的版本号大的时候,会被调用
28.         * 所以可以在这个方法中处理一些,像向数据库中的表中添加一个参数
29.         */ 
30.        @Override 
31.        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
32.            db.execSQL("alter table person add phone varchar(12) null"); 
33.        } 
34.     
35.    } 
 
之后,对数据库进行操作:
1.    public class PersonService { 
2.        private DbOpenHelper helper; 
3.     
4.        public PersonService(Context context) { 
5.            helper = new DbOpenHelper(context); 
6.        } 
7.     
8.        //向数据库中增加数据  
9.        public void addData(Person person) { 
10.            SQLiteDatabase db = helper.getWritableDatabase(); 
11.            db.execSQL("insert into person (name, age, phone) values (?, ?, ?)", 
12.                    new Object[] { person.getName(), person.getAge(), person.getPhone() }); 
13.        } 
14.     
15.        //从数据库中删除数据  
16.        public void deleteData(Integer id) { 
17.            SQLiteDatabase db = helper.getWritableDatabase(); 
18.            db.execSQL("delete from person where id = ?", new Object[] { id }); 
19.        } 
20.     
21.        //往数据库中更新数据  
22.        public void updateData(Person person) { 
23.            SQLiteDatabase db = helper.getWritableDatabase(); 
24.            db.execSQL("update person set name=?,age=?,phone=? where id = ?", 
25.                    new Object[] { person.getName(), person.getAge(), person.getPhone(), person.getId() }); 
26.        } 
27.     
28.        //查找数据库中的数据  
29.        public Person checkData(Integer id) { 
30.            SQLiteDatabase db = helper.getReadableDatabase(); 
31.            Cursor cursor = db.rawQuery("select * from person where id = ?", 
32.                    new String[] { id.toString() }); 
33.            if (cursor.moveToFirst()) { 
34.                int personid = cursor.getInt(cursor.getColumnIndex("id")); 
35.                String name = cursor.getString(cursor.getColumnIndex("name")); 
36.                String phone = cursor.getString(cursor.getColumnIndex("phone")); 
37.                short age = cursor.getShort(cursor.getColumnIndex("age")); 
38.                if(cursor != null) 
39.                    cursor.close(); 
40.                return new Person(personid, name, phone, age); 
41.            } 
42.            return null; 
43.        } 
44.     
45.        //查找数据库中的person表中的数据的数目  
46.        public int getCount() { 
47.            SQLiteDatabase db = helper.getReadableDatabase(); 
48.            Cursor cursor = db.rawQuery("select count(*) from person", null); 
49.            cursor.moveToFirst(); 
50.            int count = cursor.getInt(0); 
51.             
52.            if(cursor != null) 
53.                cursor.close(); 
54.             
55.            return count; 
56.        } 
57.     
58.        /*
59.         * 分页,就是取出数据库中跳过offset条数据的maxCount条数据,
60.         * offset代表跳过的数据的条数,如3,表示跳过3条,从第四条开始取数据
61.         * maxCount代表要取出的数据的条数
62.         */ 
63.        public List getScrollData(int offset, int maxCount) { 
64.            List persons = new ArrayList(); 
65.             
66.            SQLiteDatabase db = helper.getReadableDatabase(); 
67.            Cursor cursor = db.rawQuery("select * from person limit ?,?", 
68.                    new String[]{String.valueOf(offset), String.valueOf(maxCount)}); 
69.             
70.            while(cursor.moveToNext()){ 
71.                int personid = cursor.getInt(cursor.getColumnIndex("id")); 
72.                String name = cursor.getString(cursor.getColumnIndex("name")); 
73.                String phone = cursor.getString(cursor.getColumnIndex("phone")); 
74.                short age = cursor.getShort(cursor.getColumnIndex("age")); 
75.                persons.add(new Person(personid, name, phone, age)); 
76.            } 
77.             
78.            if(cursor != null){ 
79.                cursor.close(); 
80.            } 
81.            return persons; 
82.        } 
83.    } 
 
之后,通过单元测试来测试所编写的数据库代码:
1.    public class DbOpenHelperTest extends AndroidTestCase { 
2.        private final static String TAG = "DbOpenHelperTest"; 
3.         
4.        public void testCreateDatabase() throws Throwable{ 
5.            DbOpenHelper helper = new DbOpenHelper(this.getContext()); 
6.            helper.getWritableDatabase(); 
7.        } 
8.         
9.        public void testAddData() throws Throwable{ 
10.            PersonService service = new PersonService(this.getContext()); 
11.             
12.            Person person = new Person(); 
13.            person.setName("hanmeimei"); 
14.            person.setAge((short)12); 
15.            person.setPhone("12738487675"); 
16.            service.addData(person); 
17.             
18.            person.setName("lilei"); 
19.            person.setAge((short)15); 
20.            person.setPhone("1273834275"); 
21.            service.addData(person); 
22.             
23.            person.setName("mingming"); 
24.            person.setAge((short)34); 
25.            person.setPhone("12737857675"); 
26.            service.addData(person); 
27.             
28.            person.setName("lile"); 
29.            person.setAge((short)32); 
30.            person.setPhone("12783947675"); 
31.            service.addData(person); 
32.             
33.            person.setName("mashan"); 
34.            person.setAge((short)22); 
35.            person.setPhone("12709847675"); 
36.            service.addData(person); 
37.        } 
38.         
39.        public void testDeleteData() throws Throwable{ 
40.            PersonService service = new PersonService(this.getContext()); 
41.            service.deleteData(1); 
42.        } 
43.         
44.        public void testUpdateData() throws Throwable{ 
45.            PersonService service = new PersonService(this.getContext()); 
46.            Person person = service.checkData(3); 
47.            person.setName("linsan"); 
48.            service.updateData(person); 
49.        } 
50.         
51.        public void testCheckData() throws Throwable{ 
52.            PersonService service = new PersonService(this.getContext()); 
53.            Person person = service.checkData(3); 
54.             
55.            Log.d(TAG, person.toString()); 
56.        } 
57.         
58.        public void testGetCount() throws Throwable{ 
59.            PersonService service = new PersonService(this.getContext()); 
60.            int count = service.getCount(); 
61.            Log.d(TAG, "count = " + count); 
62.        } 
63.         
64.        public void testGetScrollData() throws Throwable{ 
65.            PersonService service = new PersonService(this.getContext()); 
66.            List persons = service.getScrollData(0, 5); 
67.            for(Person person : persons){ 
68.                Log.d(TAG, person.toString()); 
69.            } 
70.        } 
71.    }

你可能感兴趣的:(Android代码实现)