MyProvider.java

本文详细介绍了如何使用内容提供者与SQLite数据库进行交互,包括插入、删除、更新和查询操作,通过定义UriMatcher来匹配不同的操作路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package com.luofei.cp;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class MyProvider extends ContentProvider {
 MyDataBase myDB;
 SQLiteDatabase mySqliteDB;
 
 private static final UriMatcher sMatcher; 
    static{
     //常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码(-1)
        sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        //如果match()方法匹配content://com.changcheng.sqlite.provider.contactprovider/contact路径,返回匹配码为1
        sMatcher.addURI(Utils.AUTOHORITY,Utils.TNAME, Utils.ITEM);
        //如果match()方法匹配   content://com.changcheng.sqlite.provider.contactprovider/contact/230路径,返回匹配码为2
        sMatcher.addURI(Utils.AUTOHORITY, Utils.TNAME+"/#", Utils.ITEM_ID);
    }
 
 @Override
 public int delete(Uri uri, String selection, String[] selectionArgs) {
  // TODO Auto-generated method stub
  mySqliteDB = myDB.getWritableDatabase();
        int count = 0;
        switch (sMatcher.match(uri)) {
        case Utils.ITEM:
                count = mySqliteDB.delete(Utils.TNAME,selection, selectionArgs);
                break;
        case Utils.ITEM_ID:
                String id = uri.getPathSegments().get(1);
                count = mySqliteDB.delete(Utils.TID, Utils.TID+"="+id+(!TextUtils.isEmpty(Utils.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 Utils.ITEM:
                return Utils.CONTENT_TYPE;
        case Utils.ITEM_ID:
            return Utils.CONTENT_ITEM_TYPE;
        default:
                throw new IllegalArgumentException("Unknown URI"+uri);
        }
  
 }

 @Override
 public Uri insert(Uri uri, ContentValues values) {
  // TODO Auto-generated method stub
  mySqliteDB = myDB.getWritableDatabase();
        long rowId;
        if(sMatcher.match(uri)!=Utils.ITEM){
                throw new IllegalArgumentException("Unknown URI"+uri);
        }
        rowId = mySqliteDB.insert(Utils.TNAME,Utils.TID,values);
           if(rowId>0){
                   Uri noteUri=ContentUris.withAppendedId(Utils.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.myDB=new MyDataBase(this.getContext());
  return true;
 }

 @Override
 public Cursor query(Uri uri, String[] projection, String selection,
   String[] selectionArgs, String sortOrder) {
  // TODO Auto-generated method stub
  this.mySqliteDB=myDB.getWritableDatabase();
  Cursor c;
        Log.d("-------", String.valueOf(sMatcher.match(uri)));
        switch (sMatcher.match(uri)) {
        case Utils.ITEM:
                c = this.mySqliteDB.query(Utils.TNAME, projection, selection, selectionArgs, null, null, null);
        
                break;
        case Utils.ITEM_ID:
                String id = uri.getPathSegments().get(1);
                c = mySqliteDB.query(Utils.TNAME, projection, Utils.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;
 }

}

内附安装教程,含14套模板。 此源码为商业版全功能无授权版,1031全新的友价虚拟物品在线交易商城   模板源码,含14套模板带熊掌号及百度主动提交插件   源码运行环境,以下是友价虚拟物品在线交易   商城模板源码1031商业版的修复内容:   手机端:   新增手机版任务大厅功能   更新商家版会员中心界面   电脑端:   编辑商品视频栏目直接以弹窗方式展开,不用切换页面   调整保证金规则(有订单未完成,禁止解冻保证金)   新增商品问答功能   新增评价视频晒单功能   完善退款记录功能,每次退款跟处理结果都做记录(旧的只能保留最+新一次的退款记录)   支付宝等付款时,资金记录里同时记录交易号   阿里云OSS设置开关功能(后台基本设置-存储接口里)   开通阿里云OSS产品效果图也能存储的功能   友价   查看更多关于 友价 的文章   虚拟物品在线交易商城模板源码1023商业版源码安装教程   1.把程序上传到网站根目录下,不支持二级目录安装;   2.用EditPlus或者Notepad等代码编辑软件打开数据库配置文件:config/config.php 把里面的数据库信息改为自己的mysql数据库信息   文件内有标注,根据标注提示修改   3.导入数据库。把数据库目录下的shujku.sql数据库备份文件还原到你的mysql数据库内。   4.修改熊掌号地址,用editplus或者Notepad等代码编辑软件打开文件 user/baidu.php 把里面的这个域名 www.baidu.com 改为自己的   5.修改邮件通知名称,文件路径 yjadmin1/shop.php 把里面的关于 商业源码 这个名称,改为自己的,建议不要太长,否则会被屏蔽
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值