Android内容提供程序 - 使用查询方法进行插入、删除和更新
创始人
2024-10-09 10:30:35
0

以下是一个使用查询方法进行插入、删除和更新的Android内容提供程序的解决方法,包含了代码示例。

首先,创建一个自定义的ContentProvider类,继承自ContentProvider,并实现必要的方法。

public class MyContentProvider extends ContentProvider {
    // 定义数据库相关的常量
    private static final String DATABASE_NAME = "mydb";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "mytable";

    // 定义URI和URI匹配模式的常量
    private static final String AUTHORITY = "com.example.mycontentprovider";
    private static final String CONTENT_URI_BASE = "content://" + AUTHORITY;
    private static final String CONTENT_URI_STRING = CONTENT_URI_BASE + "/" + TABLE_NAME;
    public static final Uri CONTENT_URI = Uri.parse(CONTENT_URI_STRING);

    // 定义数据库操作的常量
    private static final int URI_MATCH_ALL = 1;
    private static final int URI_MATCH_ITEM = 2;

    // 定义数据库操作的帮助类
    private DatabaseHelper databaseHelper;

    // 创建数据库操作的帮助类
    private static class DatabaseHelper extends SQLiteOpenHelper {
        public DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // 创建数据库表
            String createTable = "CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY, name TEXT)";
            db.execSQL(createTable);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // 升级数据库表
            String dropTable = "DROP TABLE IF EXISTS " + TABLE_NAME;
            db.execSQL(dropTable);
            onCreate(db);
        }
    }

    @Override
    public boolean onCreate() {
        // 初始化数据库操作的帮助类
        databaseHelper = new DatabaseHelper(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        // 根据URI匹配模式进行不同的查询操作
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        Cursor cursor;
        switch (URI_MATCHER.match(uri)) {
            case URI_MATCH_ALL:
                cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case URI_MATCH_ITEM:
                String id = uri.getLastPathSegment();
                String where = "_id=" + id + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
                cursor = db.query(TABLE_NAME, projection, where, selectionArgs, null, null, sortOrder);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // 插入数据
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        long rowId = db.insert(TABLE_NAME, null, values);
        if (rowId > 0) {
            Uri insertUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(insertUri, null);
            return insertUri;
        }
        throw new SQLException("Failed to insert row into " + uri);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // 删除数据
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        int count;
        switch (URI_MATCHER.match(uri)) {
            case URI_MATCH_ALL:
                count = db.delete(TABLE_NAME, selection, selectionArgs);
                break;
            case URI_MATCH_ITEM:
                String id = uri.getLastPathSegment();
                String where = "_id=" + id + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
                count = db.delete(TABLE_NAME, where, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        // 更新数据
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        int count;
        switch (URI_MATCHER.match(uri)) {
            case URI_MATCH_ALL:
                count = db.update(TABLE_NAME, values, selection, selectionArgs);
                break;
            case URI_MATCH_ITEM:
                String id = uri.getLastPathSegment();

相关内容

热门资讯

推荐十款!poker worl... 推荐十款!poker world辅助器,xpoker辅助助手,wepoke教程(有挂解说)1、pok...
一分钟揭秘!哈糖大菠萝怎么开挂... 一分钟揭秘!哈糖大菠萝怎么开挂,sohoo辅助,必胜教程(有挂教程)暗藏猫腻,小编详细说明哈糖大菠萝...
攻略讲解!we poker免费... 攻略讲解!we poker免费辅助器(透视脚本)详细透视辅助黑科技(好像是真的有挂)亲,关键说明,w...
重大通报!聚星ai辅助工具收费... 重大通报!聚星ai辅助工具收费多少(透视脚本)详细透视辅助机制(确实真的是有挂)1、聚星ai辅助工具...
终于知道!xpoker辅助怎么... 终于知道!xpoker辅助怎么用,拱趴大菠萝作弊方法,可靠教程(有挂介绍)1、首先打开拱趴大菠萝作弊...
教学盘点!epoker免费透视... 教学盘点!epoker免费透视脚本,xpoker怎么作弊,玩家教你(有挂攻略);亲,关键说明,epo...
我来教教大家!xpoker透视... 我来教教大家!xpoker透视辅助(透视脚本)详细透视辅助挂(好像是真的有挂)1、每一步都需要思考,...
今日百科!德州之星扫描器,约局... 今日百科!德州之星扫描器,约局吧德州真的有透视挂吗,新2025教程(有挂技巧)在进入约局吧德州真的有...
玩家必看科普!德州透视脚本,x... 玩家必看科普!德州透视脚本,xpoker辅助助手,攻略方法(有挂解密);1、上手简单,内置详细流程视...
八分钟德州局透视!德州辅助工具... 八分钟德州局透视!德州辅助工具到底怎么样,智星德州辅助译码插件靠谱吗,详细教程(有挂方法);1、该软...