自定义数据库 -- ContentProvider和ContentResolver
来源:互联网 发布:蚂蚁网络兼职 编辑:程序博客网 时间:2024/05/18 03:59
在android中自己通过ContentProvider创建一个上述数据表。
1. 首先通过SQLiteOpenHelper执行SQL语句,创建数据表。
private static final String DATABASE_NAME = "simrecord.db";private static final int DATABASE_VERSION = 1;<p>private static final String SIMCARD_TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + //“CREATE TABLE ”后面需要加一个空格"(_id INTEGER primary key," +" card_id VARCHAR," +"date VARCHAR," +"sim_state VARCHAR," +"sim_operator VARCHAR," +"sim_country VARCHAR," +"data_state VARCHAR," +"network_type VARCHAR)";</p>
<pre class="java" name="code">private class MyDataBaseHelper extends SQLiteOpenHelper{ //by SQLiteOpenHelper to make SQLiteDatabasepublic MyDataBaseHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);// TODO Auto-generated constructor stub}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubLog.d(TAG,"MyDataBaseHelper onCreate()");db.execSQL(SIMCARD_TABLE_CREATE);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stubLog.d(TAG,"MyDataBaseHelper onUpgrade()");}}
2.然后通过继承ContentProvider类
其中onCreate()方法,通过SQLiteOpenHelper类的getWritableDatabase()方法获取数据库SQLiteDatabase类mDb,然后通过mDb对数据库进行各项操作。
覆写其中查询/删除/插入/增加方法,通过mDb对数据表进行相应的操作。
public class SimRecordProvider extends ContentProvider {private static final String TAG ="simrecord: SimRecordProvider";private static final String PROVIDER_NAME ="com.sec.provider.simrecord";private static final String TABLE_NAME = "simrecord";private MyDataBaseHelper mDataBaseHelper;private SQLiteDatabase mDb;@Overridepublic boolean onCreate() {// TODO Auto-generated method stubLog.d(TAG," onCreate()");mDataBaseHelper =new MyDataBaseHelper(getContext());mDb = mDataBaseHelper.getWritableDatabase();return true;}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {// TODO Auto-generated method stubLog.d(TAG," delete()");//Log.d(TAG," delete() where : " +selection);mDb.delete(TABLE_NAME, selection, null);return 0;}@Overridepublic String getType(Uri uri) {// TODO Auto-generated method stubLog.d(TAG," getType()");return null;}@Overridepublic Uri insert(Uri uri, ContentValues values) {// TODO Auto-generated method stubLog.d(TAG," insert()");//Log.d(TAG," insert() values : " +values);mDb.insert(TABLE_NAME, null, values);return null;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection, String[] seletionArgs,String sortOrder) {// TODO Auto-generated method stubLog.d(TAG," query()");//Log.d(TAG," query() where : " +selection);//String where = "card_id=?";Cursor mCursor =null;mCursor = mDb.query(TABLE_NAME, null, selection, seletionArgs, null, null, null);return mCursor;}@Overridepublic int update(Uri uri, ContentValues values, String selection, String[] seletionArgs) {// TODO Auto-generated method stubLog.d(TAG," update()");//Log.d(TAG," update() where : " +selection);mDb.update(TABLE_NAME, values, selection, seletionArgs);return 0;}}
3.在AndroidManifest.xml文件中添加SimRecordProvider.java的authorities, 这样子ContentResolver才可以通过URI查找到该数据库
<provider android:name=".SimRecordProvider" android:authorities="com.sec.provider.simrecord" android:exported="true"> </provider>
4.ContentResolver类其实就是提供给用户进行操作数据库的接口。
通过context.getContentResolver() 获取ContentResolver,ContentResolver类中的方法:query()/delete()/insert()/update()分别对应着ContentProvider类中的相应方法。
CONTENT_URI相当于ContentProvider类中的地址,ContentResolver通过CONTENT_URI查找到对应的ContentProvider。
private static final String CONTENT_URI ="content://com.sec.provider.simrecord";mContentResolver = context.getContentResolver();
查询数据表中有多少条数据:
public int queryCount (){ //Log.d(TAG," queryCount()****************Begin");Cursor mCursor = mContentResolver.query(Uri.parse(CONTENT_URI), null, null, null, null);if(mCursor==null){return 0;}//Log.d(TAG," queryCount() mCursor = " + mCursor);Log.d(TAG," queryCount() mCursor.getCount() = " + mCursor.getCount());int count = mCursor.getCount();return count;}
插入新的卡数据:通过ContentValues类封装需要保存的数据。
public void insertCard(String card_id, ContentValues values){//Log.d(TAG," insertCard()****************Begin");//Log.d(TAG," insertCard() card_id = " + card_id);if(!isNewCard(card_id)){Toast.makeText(mContext, "the card_id is alread exsit", Toast.LENGTH_SHORT).show();Log.d(TAG," insertCard()**the card_id is alread exsit");return ;}mContentResolver.insert(Uri.parse(CONTENT_URI), values);Log.d(TAG," insertCard()** the card inserted in database");Toast.makeText(mContext, "insert card_id="+card_id, Toast.LENGTH_SHORT).show();}
删除指定card_id的卡数据:
public void deleteCard(String card_id){if(card_id != null){String where = "card_id='"+card_id+"'"; // where = '123456'Log.d(TAG," deleteCard() where= "+where );mContentResolver.delete(Uri.parse(CONTENT_URI), where, null);}}
0 0
- 自定义数据库 -- ContentProvider和ContentResolver
- 数据库存取操作,ContentProvider和ContentResolver
- ContentProvider和ContentResolver
- ContentProvider和ContentResolver
- ContentProvider ,ContentObserver 和ContentResolver
- Andorid ContentProvider和ContentResolver
- contentProvider和contentResolver
- ContentResolver和ContentProvider
- ContentResolver和ContentProvider
- ContentProvider和ContentResolver
- android之ContentResolver和ContentProvider
- ContentProvider/ContentResolver和Uri详解
- 数据共享ContentProvider和ContentResolver
- ContentResolver和ContentProvider的关系
- ContentProvider和ContentResolver的混用
- ContentProvider和ContentResolver的使用
- ContentProvider和ContentResolver的学习
- 简单的Contentprovider和contentResolver
- .Net Mvc validateRequest设置为false不起作用的解决方案
- MATLAB数字图像处理从入门到精通(持续更新)
- 尽力而为还不够
- poj 3083 dfs
- SQL中Group By的使用
- 自定义数据库 -- ContentProvider和ContentResolver
- DSL,
- RocketMQ使用笔记
- logcat的调试 比较有用的几个命令加安卓AM命令说明
- 学习PHP的过程记录(5)
- NYOJ Problem of IP
- iOS开发之监听键盘高度的变化
- bzoj3572_虚树的构建+lca
- C# 获得List<int>众数