"在\nclass DatabaseProvider : ContentProvider() {\n\tprivate val bookDir = 0\n\tprivate val bookItem = 1\n\tprivate val categoryDir = 2\n\tprivate val categoryItem = 3\n\tprivate val authority = "com.example.databasetest1.provider"\n\tprivate var dbHelper: MyDatabaseHelper? = null\n\n\tprivate val uriMatcher by lazy {\n\t val matcher = UriMatcher(UriMatcher.NO_MATCH)\n\t matcher.addURI(authority, "book", bookDir)\n\t matcher.addURI(authority, "book/#", bookItem)\n\t matcher.addURI(authority, "category", categoryDir)\n\t matcher.addURI(authority, "category/#", categoryItem)\n\t matcher\n\t}\n\n\toverride fun onCreate(): Boolean {\n\t context?.let {\n\t dbHelper = MyDatabaseHelper(it, "BookStore.db", 2)\n\t true\n\t }\n\t return false\n\t}\n\n\toverride fun query(uri: Uri, projection: Array?, selection: String?,\n\t selectionArgs: Array?, sortOrder: String?): Cursor? {\n\t dbHelper?.let {\n\t val db = it.readableDatabase\n\t return when (uriMatcher.match(uri)) {\n\t bookDir -> db.query("Book", projection, selection, selectionArgs, null, null, sortOrder)\n\t bookItem -> {\n\t val bookId = uri.pathSegments[1]\n\t db.query("Book", projection, "id = ?", arrayOf(bookId), null, null, sortOrder)\n\t }\n\t categoryDir -> db.query("Category", projection, selection, selectionArgs, null, null, sortOrder)\n\t categoryItem -> {\n\t val categoryId = uri.pathSegments[1]\n\t db.query("Category", projection, "id = ?", arrayOf(categoryId), null, null, sortOrder)\n\t }\n\t else -> null\n\t }\n\t }\n\t return null\n\t}\n\n\toverride fun insert(uri: Uri, values: ContentValues?): Uri? {\n\t dbHelper?.let {\n\t val db = it.writableDatabase\n\t return when (uriMatcher.match(uri)) {\n\t bookDir, bookItem -> {\n\t val newBookId = db.insert("Book", null, values)\n\t Uri.parse("content://$authority/book/$newBookId")\n\t }\n\t categoryDir, categoryItem -> {\n\t val newCategoryId = db.insert("Category", null, values)\n\t Uri.parse("content://$authority/category/$newCategoryId")\n\t }\n\t else -> null\n\t }\n\t }\n\t return null\n\t}\n\n\toverride fun update(uri: Uri, values: ContentValues?, selection: String?,\n\t selectionArgs: Array?): Int {\n\t dbHelper?.let {\n\t val db = it.writableDatabase\n\t return when (uriMatcher.match(uri)) {\n\t bookDir -> db.update("Book", values, selection, selectionArgs)\n\t bookItem -> {\n\t val bookId = uri.pathSegments[1]\n\t db.update("Book", values, "id = ?", arrayOf(bookId))\n\t }\n\t categoryDir -> db.update("Category", values, selection, selectionArgs)\n\t categoryItem -> {\n\t val categoryId = uri.pathSegments[1]\n\t db.update("Category", values, "id = ?", arrayOf(categoryId))\n\t }\n\t else -> 0\n\t }\n\t }\n\t return 0\n\t}\n\n\toverride fun delete(uri: Uri, selection: String?, selectionArgs: Array?): Int {\n\t dbHelper?.let {\n\t val db = it.writableDatabase\n\t return when (uriMatcher.match(uri)) {\n\t bookDir -> db.delete("Book", selection, selectionArgs)\n\t bookItem -> {\n\t val bookId = uri.pathSegments[1]\n\t db.delete("Book", "id = ?", arrayOf(bookId))\n\t }\n\t categoryDir -> db.delete("Category", selection, selectionArgs)\n\t categoryItem -> {\n\t val categoryId = uri.pathSegments[1]\n\t db.delete("Category", "id = ?", arrayOf(categoryId))\n\t }\n\t else -> 0\n\t }\n\t }\n\t return 0\n\t}\n\n\toverride fun getType(uri: Uri): String? {\n\t return when (uriMatcher.match(uri)) {\n\t bookDir -> "vnd.android.cursor.dir/vnd.com.example.databasetest1.provider.book"\n\t bookItem -> "vnd.android.cursor.item/vnd.com.example.databasetest1.provider.book"\n\t categoryDir -> "vnd.android.cursor.dir/vnd.com.example.databasetest1.provider.category"\n\t categoryItem -> "vnd.android.cursor.item/vnd.com.example.databasetest1.provider.category"\n\t else -> null\n\t }\n\t}\n}\n中\nkt如何利用ContentResolver访问ContentProvider内容:要利用ContentResolver访问ContentProvider,可以通过以下步骤:\n\n1. 获取ContentResolver实例:\n\t\n\tval contentResolver = context.contentResolver\n\t\n\n2. 构建查询Uri:\n\t\n\tval uri = Uri.parse(\"content://com.example.databasetest1.provider/book\")\n\t\n\n3. 执行查询操作:\n\t\n\tval projection = arrayOf(\"id\", \"name\", \"author\")\n\tval selection = \"author = ?\"\n\tval selectionArgs = arrayOf(\"John Doe\")\n\tval sortOrder = \"name ASC\"\n\t\n\tval cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)\n\t\n\n4. 处理查询结果:\n\t\n\tif (cursor != null) {\n\t while (cursor.moveToNext()) {\n\t val id = cursor.getString(cursor.getColumnIndex(\"id\"))\n\t val name = cursor.getString(cursor.getColumnIndex(\"name\"))\n\t val author = cursor.getString(cursor.getColumnIndex(\"author\"))\n\t \n\t // 处理查询结果\n\t }\n\t cursor.close()\n\t}\n\t\n\n5. 执行插入操作:\n\t\n\tval values = ContentValues().apply {\n\t put(\"name\", \"Book Name\")\n\t put(\"author\", \"John Doe\")\n\t}\n\t\n\tval insertedUri = contentResolver.insert(uri, values)\n\t\n\n6. 执行更新操作:\n\t\n\tval values = ContentValues().apply {\n\t put(\"name\", \"New Book Name\")\n\t}\n\t\n\tval updatedRows = contentResolver.update(uri, values, \"id = ?\", arrayOf(\"1\"))\n\t\n\n7. 执行删除操作:\n\t\n\tval deletedRows = contentResolver.delete(uri, \"id = ?\", arrayOf(\"1\"))\n\t\n\n通过ContentResolver可以方便地与ContentProvider进行数据交互,实现读取、插入、更新和删除操作。记得根据实际情况构建正确的Uri和ContentValues。\n

Android ContentProvider 使用教程:利用 ContentResolver 访问数据库

原文地址: https://www.cveoy.top/t/topic/qjDj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录