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

二、代码举例:

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

5b1e38fc-929c-4587-a2b2-87f060663db5

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 }
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值