您的位置:首页 > 移动开发 > Android开发

Android组件系列----ContentProvider内容提供者【2】

2014-11-19 14:26 495 查看
二、代码举例:

最终所有工程文件的目录结构如下:





PersonDao是增删改查数据库的工具类,并在PersonContentProvider中得到调用。DBHelper用于初始化SQLite数据库。

PersonContentProvider用于向外提供增删改查的接口。并最终在ContentResolverTest的Test.java中进行单元测试,实现CRUD。

下面来看一下具体的实现步骤。

新建工程文件ContetProviderTest01。

(1)新建类PersonDao:用于进行对SQLite的CRUD操作。代码如下:

PersonDao.java:



1 package com.example.contentprovidertest01.dao;
2
3 import android.content.ContentValues;
4 import android.content.Context;
5 import android.database.Cursor;
6 import android.database.sqlite.SQLiteDatabase;
7
8 import com.example.contentprovidertest01.db.DBHelper;
9
10 public class PersonDao {
11     private DBHelper helper = null;
12
13     public PersonDao(Context context) {
14         helper = new DBHelper(context);
15     }
16
17     //方法:插入操作,返回的long类型为:插入当前行的行号
18     public long insertPerson(ContentValues values) {
19         long id = -1;
20         SQLiteDatabase database = null;
21         try {
22             database = helper.getWritableDatabase();
23             id = database.insert("person", null, values);
24         } catch (Exception e) {
25             e.printStackTrace();
26         } finally {
27             if (database != null) {
28                 database.close();
29             }
30         }
31         return id;
32     }
33
34     public int deletePerson(String whereClause, String[] whereArgs) {
35         int count = -1;
36         SQLiteDatabase database = null;
37         try {
38             database = helper.getWritableDatabase();
39             count = database.delete("person", whereClause, whereArgs);
40         } catch (Exception e) {
41             e.printStackTrace();
42         } finally {
43             if (database != null) {
44                 database.close();
45             }
46         }
47         return count;
48     }
49
50     public int updatePerson(ContentValues values, String whereClause,
51             String[] whereArgs) {
52         SQLiteDatabase database = null;
53         int count = -1;
54         try {
55             database = helper.getWritableDatabase();
56             count = database.update("person", values, whereClause, whereArgs);
57         } catch (Exception e) {
58             e.printStackTrace();
59         } finally {
60             if (null != database) {
61                 database.close();
62             }
63         }
64         return count;
65     }
66
67     public Cursor queryPersons(String selection, String[] selectionArgs) {
68         SQLiteDatabase database = null;
69         Cursor cursor = null;
70         try {
71             database = helper.getReadableDatabase();
72             cursor = database.query(true, "person", null, selection,
73                     selectionArgs, null, null, null, null);
74         } catch (Exception e) {
75             e.printStackTrace();
76         } finally {
77             if (null != database) {
78                 // database.close();
79             }
80         }
81         return cursor;
82     }
83
84 }


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: