database---query中selection用法和selectionArgs用法

本文介绍了一种使用SQL进行数据库查询的方法,特别关注了如何通过构造特定的条件语句来精确地定位到所需的数据记录。
public void doQuery(long id, final String name) {  
  mDb.query("some_table", // table name   
        null, // columns  
        "id=" + id + " AND name=?", // selection  
        new String[] {name}, //selectionArgs  
         //...... 更多参数省略  
  );  
  // ...... 更多代码  
} 

 

public void doQuery(long id, final String name) {  
  mDb.query("some_table", // table name   
        null, // columns  
        "id=" + id + " AND name=?", // selection  
        new String[] {name}, //selectionArgs  
         //...... 更多参数省略  
  );  
  // ...... 更多代码  
} 

把以下Android代码翻译成Java版本:class DatabaseProvider : ContentProvider() { private val bookDir = 0 private val bookItem = 1 private val categoryDir = 2 private val categoryItem = 3 private val authority = "com.example.databasetest.provider" private var dbHelper: MyDatabaseHelper? = null private val uriMatcher by lazy { val matcher = UriMatcher(UriMatcher.NO_MATCH) matcher.addURI(authority, "book", bookDir) matcher.addURI(authority, "book/#", bookItem) matcher.addURI(authority, "category", categoryDir) matcher.addURI(authority, "category/#", categoryItem) matcher } override fun onCreate() = context?.let { dbHelper = MyDatabaseHelper(it, "BookStore.db", 2) true } ?: false www.blogss.cn override fun query(uri: Uri, projection: Array<String>?, selection: String?, selectionArgs: Array<String>?, sortOrder: String?) = dbHelper?.let { // 查询数据 val db = it.readableDatabase val cursor = when (uriMatcher.match(uri)) { bookDir -> db.query("Book", projection, selection, selectionArgs, null, null, sortOrder) bookItem -> { val bookId = uri.pathSegments[1] db.query("Book", projection, "id = ?", arrayOf(bookId), null, null, sortOrder) } categoryDir -> db.query("Category", projection, selection, selectionArgs, null, null, sortOrder) categoryItem -> { val categoryId = uri.pathSegments[1] db.query("Category", projection, "id = ?", arrayOf(categoryId), null, null, sortOrder) } else -> null } cursor } override fun insert(uri: Uri, values: ContentValues?) = dbHelper?.let { // 添加数据 val db = it.writableDatabase val uriReturn = when (uriMatcher.match(uri)) { bookDir, bookItem -> { val newBookId = db.insert("Book", null, values) Uri.parse("content://$authority/book/$newBookId") } categoryDir, categoryItem -> { val newCategoryId = db.insert("Category", null, values) Uri.parse("content://$authority/category/$newCategoryId") } else -> null } uriReturn } override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<String>?) = dbHelper?.let { // 更新数据 val db = it.writableDatabase val updatedRows = when (uriMatcher.match(uri)) { bookDir -> db.update("Book", values, selection, selectionArgs) bookItem -> { val bookId = uri.pathSegments[1] db.update("Book", values, "id = ?", arrayOf(bookId)) } categoryDir -> db.update("Category", values, selection, selectionArgs) categoryItem -> { val categoryId = uri.pathSegments[1] db.update("Category", values, "id = ?", arrayOf(categoryId)) } else -> 0 } updatedRows } ?: 0 override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?) = dbHelper?.let { // 删除数据 www.blogss.cn val db = it.writableDatabase val deletedRows = when (uriMatcher.match(uri)) { bookDir -> db.delete("Book", selection, selectionArgs) bookItem -> { val bookId = uri.pathSegments[1] db.delete("Book", "id = ?", arrayOf(bookId)) } categoryDir -> db.delete("Category", selection, selectionArgs) categoryItem -> { val categoryId = uri.pathSegments[1] db.delete("Category", "id = ?", arrayOf(categoryId)) } else -> 0 } deletedRows } ?: 0 override fun getType(uri: Uri) = when (uriMatcher.match(uri)) { bookDir -> "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.book" bookItem -> "vnd.android.cursor.item/vnd.com.example.databasetest.provider.book" categoryDir -> "vnd.android.cursor.dir/vnd.com.example.databasetest. provider.category" categoryItem -> "vnd.android.cursor.item/vnd.com.example.databasetest. provider.category" else -> null } }
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值