SQLiteDatabase和Contentprovider
SQLiteDatabase和Contentprovider这两个数据库,我一般是用前面一个,喜欢它的操作数据库的语句,简单明了,可惜有时遇到数据库同步的问题,有时我们需要在一个数据库下建立多个表,多个Activity都要访问到数据库。最近就遇到过这个问题,虽然应用没有死掉,但有时报错也不太爽,报的警告如下
A SQLiteConnection object for database '/storage/sdcard0/vpnmsgdb.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
不过虽然警告是报了,我暂时也没有改,虽然Contentprovider这个接口可以解决同步的问题,也许是一直都是用SQLiteDatabase的原因吧,个人觉得只要应用不死掉应该问题不大,
在网上找到有Contentprovider的例子。
原文在http://www.cnblogs.com/chenglong/articles/1892029.html
贴上原文
一、ContentProvider简介
当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据。虽然使用其他方法也可以对外共享数据,但数据访问方式会因数据存储的方式而不同,如:采用文件方式对外共享数据,需要进行文件操作读写数据;采用sharedpreferences共享数据,需要使用sharedpreferences API读写数据。而使用ContentProvider共享数据的好处是统一了数据访问方式。
二、Uri类简介
Uri代表了要操作的数据,Uri主要包含了两部分信息:1.需要操作的ContentProvider ,2.对ContentProvider中的什么数据进行操作,一个Uri由以下几部分组成:
1.scheme:ContentProvider(内容提供者)的scheme已经由Android所规定为:content://。
2.主机名(或Authority):用于唯一标识这个ContentProvider,外部调用者可以根据这个标识来找到它。
3.路径(path):可以用来表示我们要操作的数据,路径的构建应根据业务而定,如下:
• 要操作contact表中id为10的记录,可以构建这样的路径:/contact/10
• 要操作contact表中id为10的记录的name字段, contact/10/name
• 要操作contact表中的所有记录,可以构建这样的路径:/contact
要操作的数据不一定来自数据库,也可以是文件等他存储方式,如下:
要操作xml文件中contact节点下的name节点,可以构建这样的路径:/contact/name
如果要把一个字符串转换成Uri,可以使用Uri类中的parse()方法,如下:
Uri uri = Uri.parse("content://com.changcheng.provider.contactprovider/contact")
三、UriMatcher、ContentUrist和ContentResolver简介
因为Uri代表了要操作的数据,所以我们很经常需要解析Uri,并从Uri中获取数据。Android系统提供了两个用于操作Uri的工具类,分别为UriMatcher 和ContentUris 。掌握它们的使用,会便于我们的开发工作。
UriMatcher:用于匹配Uri,它的用法如下:
1.首先把你需要匹配Uri路径全部给注册上,如下:
//常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码(-1)。
UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//如果match()方法匹配content://com.changcheng.sqlite.provider.contactprovider/contact路径,返回匹配码为1
uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact”, 1);//添加需要匹配uri,如果匹配就会返回匹配码
//如果match()方法匹配 content://com.changcheng.sqlite.provider.contactprovider/contact/230路径,返回匹配码为2
uriMatcher.addURI(“com.changcheng.sqlite.provider.contactprovider”, “contact/#”, 2);//#号为通配符
2.注册完需要匹配的Uri后,就可以使用uriMatcher.match(uri)方法对输入的Uri进行匹配,如果匹配就返回匹配码,匹配码是调用addURI()方法传入的第三个参数,假设匹配content://com.changcheng.sqlite.provider.contactprovider/contact路径,返回的匹配码为1。
ContentUris:用于获取Uri路径后面的ID部分,它有两个比较实用的方法:
• withAppendedId(uri, id)用于为路径加上ID部分
• parseId(uri)方法用于从路径中获取ID部分
ContentResolver:当外部应用需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,可以使用ContentResolver 类来完成,要获取ContentResolver 对象,可以使用Activity提供的getContentResolver()方法。 ContentResolver使用insert、delete、update、query方法,来操作数据。
四、ContentProvider示例程序
Manifest.xml中的代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<application android:icon="@drawable/icon"android:label="@string/app_name">
<activity android:name=".TestWebviewDemo"android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<data android:mimeType="vnd.android.cursor.dir/vnd.ruixin.login"/>
</intent-filter>
<intent-filter>
<data android:mimeType="vnd.android.cursor.item/vnd.ruixin.login"/>
</intent-filter>
</activity>
<provider android:name="MyProvider"android:authorities="com.ruixin.login"/>
</application>
|
需要在<application></application>中为provider进行注册!!!!
首先定义一个数据库的工具类:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public classRuiXin {
publicstaticfinal String DBNAME = "ruixinonlinedb";
publicstaticfinal String TNAME = "ruixinonline";
publicstaticfinal intVERSION = 3;
publicstaticString TID = "tid";
publicstaticfinal String EMAIL = "email";
publicstaticfinal String USERNAME = "username";
publicstaticfinal String DATE = "date";
publicstaticfinal String SEX = "sex";
publicstaticfinal String AUTOHORITY = "com.ruixin.login";
publicstaticfinal intITEM = 1;
publicstaticfinal intITEM_ID = 2;
publicstaticfinal String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.ruixin.login";
publicstaticfinal String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.ruixin.login";
}
|
- 然后创建一个数据库:
1234567891011121314151617181920212223242526272829
public
class
DBlite
extends
SQLiteOpenHelper {
public
DBlite(Context context) {
super
(context, RuiXin.DBNAME,
null
, RuiXin.VERSION);
// TODO Auto-generated constructor stub
}
@Override
public
void
onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table "
+RuiXin.TNAME+
"("
+
RuiXin.TID+
" integer primary key autoincrement not null,"
+
RuiXin.EMAIL+
" text not null,"
+
RuiXin.USERNAME+
" text not null,"
+
RuiXin.DATE+
" interger not null,"
+
RuiXin.SEX+
" text not null);"
);
}
@Override
public
void
onUpgrade(SQLiteDatabase db,
int
oldVersion,
int
newVersion) {
// TODO Auto-generated method stub
}
public
void
add(String email,String username,String date,String sex){
SQLiteDatabase db = getWritableDatabase();
ContentValues values =
new
ContentValues();
values.put(RuiXin.EMAIL, email);
values.put(RuiXin.USERNAME, username);
values.put(RuiXin.DATE, date);
values.put(RuiXin.SEX, sex);
db.insert(RuiXin.TNAME,
""
,values);
}}
- 接着创建一个Myprovider.java对数据库的接口进行包装:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
public
class
MyProvider
extends
ContentProvider{
DBlite dBlite;
SQLiteDatabase db;
private
static
final
UriMatcher sMatcher;
static
{
sMatcher =
new
UriMatcher(UriMatcher.NO_MATCH);
sMatcher.addURI(RuiXin.AUTOHORITY,RuiXin.TNAME, RuiXin.ITEM);
sMatcher.addURI(RuiXin.AUTOHORITY, RuiXin.TNAME+
"/#"
, RuiXin.ITEM_ID);
}
@Override
public
int
delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
db = dBlite.getWritableDatabase();
int
count =
0
;
switch
(sMatcher.match(uri)) {
case
RuiXin.ITEM:
count = db.delete(RuiXin.TNAME,selection, selectionArgs);
break
;
case
RuiXin.ITEM_ID:
String id = uri.getPathSegments().get(
1
);
count = db.delete(RuiXin.TID, RuiXin.TID+
"="
+id+(!TextUtils.isEmpty(RuiXin.TID=
"?"
)?
"AND("
+selection+
')'
:
""
), selectionArgs);
break
;
default
:
throw
new
IllegalArgumentException(
"Unknown URI"
+uri);
}
getContext().getContentResolver().notifyChange(uri,
null
);
return
count;
}
@Override
public
String getType(Uri uri) {
// TODO Auto-generated method stub
switch
(sMatcher.match(uri)) {
case
RuiXin.ITEM:
return
RuiXin.CONTENT_TYPE;
case
RuiXin.ITEM_ID:
return
RuiXin.CONTENT_ITEM_TYPE;
default
:
throw
new
IllegalArgumentException(
"Unknown URI"
+uri);
}
}
@Override
public
Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
db = dBlite.getWritableDatabase();
long
rowId;
if
(sMatcher.match(uri)!=RuiXin.ITEM){
throw
new
IllegalArgumentException(
"Unknown URI"
+uri);
}
rowId = db.insert(RuiXin.TNAME,RuiXin.TID,values);
if
(rowId>
0
){
Uri noteUri=ContentUris.withAppendedId(RuiXin.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(noteUri,
null
);
return
noteUri;
}
throw
new
IllegalArgumentException(
"Unknown URI"
+uri);
}
@Override
public
boolean
onCreate() {
// TODO Auto-generated method stub
this
.dBlite =
new
DBlite(
this
.getContext());// db = dBlite.getWritableDatabase();// return (db == null)?false:true;
return
true
;
}
@Override
public
Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
db = dBlite.getWritableDatabase();
Cursor c;
Log.d(
"-------"
, String.valueOf(sMatcher.match(uri)));
switch
(sMatcher.match(uri)) {
case
RuiXin.ITEM:
c = db.query(RuiXin.TNAME, projection, selection, selectionArgs,
null
,
null
,
null
);
break
;
case
RuiXin.ITEM_ID:
String id = uri.getPathSegments().get(
1
);
c = db.query(RuiXin.TNAME, projection, RuiXin.TID+
"="
+id+(!TextUtils.isEmpty(selection)?
"AND("
+selection+
')'
:
""
),selectionArgs,
null
,
null
, sortOrder);
break
;
default
:
Log.d(
"!!!!!!"
,
"Unknown URI"
+uri);
throw
new
IllegalArgumentException(
"Unknown URI"
+uri);
}
c.setNotificationUri(getContext().getContentResolver(), uri);
return
c;
}
@Override
public
int
update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return
0
;
}}最后创建测试类:
123456789101112131415161718192021222324252627282930public
class
Test
extends
Activity {
/** Called when the activity is first created. */
private
DBlite dBlite1 =
new
DBlite(
this
);;
private
ContentResolver contentResolver;
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
//先对数据库进行添加数据
dBlite1.add(email,username,date,sex);
//通过contentResolver进行查找
contentResolver = TestWebviewDemo.
this
.getContentResolver();
Cursor cursor = contentResolver.query(
RuiXin.CONTENT_URI,
new
String[] {
RuiXin.EMAIL, RuiXin.USERNAME,
RuiXin.DATE,RuiXin.SEX },
null
,
null
,
null
);
while
(cursor.moveToNext()) {
Toast.makeText(
TestWebviewDemo.
this
,
cursor.getString(cursor.getColumnIndex(RuiXin.EMAIL))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.USERNAME))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.DATE))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.SEX)),
Toast.LENGTH_SHORT).show();
}
startManagingCursor(cursor);
//查找后关闭游标
}
}注:上面是在一个程序中进行的测试,也可以再新建一个工程来模拟一个新的程序,然后将上面查询的代码加到新的程序当中!这样就模拟了contentprovider的数据共享功能了!
新建个工程:TestProvider
创建一个测试的activity123456789101112131415161718192021222324252627public
class
Test
extends
Activity {
/** Called when the activity is first created. */
private
ContentResolver contentResolver;
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过contentResolver进行查找
contentResolver = TestWebviewDemo.
this
.getContentResolver();
Cursor cursor = contentResolver.query(
RuiXin.CONTENT_URI,
new
String[] {
RuiXin.EMAIL, RuiXin.USERNAME,
RuiXin.DATE,RuiXin.SEX },
null
,
null
,
null
);
while
(cursor.moveToNext()) {
Toast.makeText(TestWebviewDemo.
this
,
cursor.getString(cursor.getColumnIndex(RuiXin.EMAIL))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.USERNAME))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.DATE))
+
" "
+ cursor.getString(cursor.getColumnIndex(RuiXin.SEX)),
Toast.LENGTH_SHORT).show();
}
startManagingCursor(cursor);
//查找后关闭游标
}
}运行此程序就能实现共享数据查询了!
注:新建的程序中的manifest.xml中不需要对provider进行注册,直接运行就行,否则会报错!
SQLiteDatabase和Contentprovider的更多相关文章
- (转)Android学习进阶路线导航线路(Android源码分享)
转载请注明出处:http://blog.csdn.net/qinjuning 前言:公司最近来了很多应届实习生,看着他们充满信心但略带稚气的脸庞上,想到了去年的自己,那是的我是不是也和 现在的他们一 ...
- Android学习进阶路线导航线路(Android源码分享)
转 ...
- 如何学习Android系统源码(转)
一. Android系统的源代码非常庞大和复杂,我们不能贸然进入,否则很容易在里面迷入方向,进而失去研究它的信心.我们应该在分析它的源代码之前学习好一些理论知识,下面就介绍一些与Android系统相关 ...
- Android中个人推崇的数据库使用方式
手机应用开发中常常会使用到数据库存储一些资料或者进行数据缓存,android中为我们提供了一个轻量的数据库.在上层进行了一层封装,同一时候还为我们提供了ContentProvider的框架.方便我们进 ...
- Android Cursor浅析
1. 本文目的 Android ContentProvider提供了进程间数据交换的一种机制.而数据库的查询就是这样的机制的应用.那么app通过Uri查询数据库而得到的Cursor到底是个什么东西?为 ...
- Android之ContentProvider数据存储
一.ContentProvider保存数据介绍 一个程序可以通过实现一个ContentProvider的抽象接口将自己的数据完全暴露出去,而且ContentProvider是以类似数据库中表的方式将数 ...
- Xamarin.Android之ContentProvider
一.前言 掌握了如何使用SQLiteOpenHelper之后,我们就可以进行下一步的学习.本章我们将会学习如何使用ContentProvider来将数据库方面的操作封装起来,同时它还可以供其他应用访问 ...
- ContentProvider域名替换小工具
开发项目域名想怎么换就怎么换,就是这么任性! 这是一个很有意思的小工具! 这是一个方便开发人员和测试人员的小工具!! 吐槽: 一直在做Android开发,一直总有一个问题存在:做自己公司的apk开发时 ...
- 简单的学习心得:网易云课堂Android开发第六章SQLite与ContentProvider
一.SQLite 1.基本操作: (1)创建数据库:在SQLiteOpenHelper的子类构造器中创建. (2)创建表:在SQLiteOpenHelper的子类onCreate方法中,调用execS ...
随机推荐
- Linux IP代理筛选系统(shell+proxy)
代理的用途 其实,除了抓取国外网页需要用到IP代理外,还有很多场景会用到代理: 通过代理访问一些国外网站,绕过被某国防火墙过滤掉的网站 使用教育网的代理服务器,可以访问到大学或科研院所的内部网站资源 ...
- CloudStack搭建KVM环境
软件环境:agent:CentOS 6.3,minimal安装,CPU启用VT management server:CentOS 6.3,minimal安装 存储:CentOS 6.3 搭建在mana ...
- IT行业,需要经常锻炼,开篇从本钱开始
今天下完班,和部门兄弟一起去打了两小时乒乓球,大汗淋漓,很痛快. 败给了两个高手,感觉年龄大了些,灵活性没有以前那么好了. 想想以前读书时,在整个学校都叱诧风云,如今即败给了几个老手,唉. 看来以后要 ...
- ExtJs4 笔记(11) Ext.ListView、Ext.view.View 数据视图
本篇介绍两个用来展示数据的容器控件,分别是Ext.ListView和Ext.view.View.Ext.ListView就是大名鼎鼎的Ext GridPanel的前身,不过现在的Ext4已经将它整合到 ...
- ABP启动配置
ABP启动配置 返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行 ...
- Android应用开发学习笔记之播放视频
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 在Android中,我们可以通过使用VideoView类或者MediaPlayer类来播放视频. 一.使用Video ...
- 理想非常丰满,现实非常骨感——致WiFi通话
WiFi通话一词,近来火热,国外,iOS 8系统測试版新增WiFi通话功能,英国运营商也着手WiFi免费通话,国内也没落下,阿里发布的170资费方案中就包含WiFi免费通话. 近日,据外媒报道,在美国 ...
- 深入理解 JBoss 7/WildFly Standalone 模式启动过程
概述 JBoss 7/WildFly Standalone 模式启动过程大致例如以下: 启动脚本 standalone.sh 启动 JBoss Modules,JBoss Modules 启动 JBo ...
- STL内存管理器的分配策略
STL提供了很多泛型容器,如vector,list和map.程序员在使用这些容器时只需关心何时往容器内塞对象,而不用关心如何管理内存,需要用多少内存,这些STL容器极大地方便了C++程序的编写.例如可 ...
- C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)
STL实践与分析 --再谈string类型(下) 四.string类型的查找操作 string类型提供了6种查找函数,每种函数以不同形式的find命名.这些操作所有返回string::size_typ ...