首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

SQLiteDatabase跟Contentprovider

2013-09-05 
SQLiteDatabase和ContentproviderSQLiteDatabase和Contentprovider这两个数据库,我一般是用前面一个,喜欢

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简介
       当123456789101112131415161718192021222324252627public class Test extendsActivity {    /** Called when the activity is first created. */        privateContentResolver contentResolver;                    publicvoid 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进行注册,直接运行就行,否则会报错!

热点排行