美文网首页
android 日常(十九)

android 日常(十九)

作者: ncd | 来源:发表于2017-01-03 15:22 被阅读35次
    • android ContentResolver 对数据库操作 in/not in的用法
    List<String> threadIdList = new ArrayList<String>();
            List<String> parameters = new ArrayList<String>();
            Uri uri = MessageSQL.CONTENT_URI;
            String[] projection = {new String("distinct "+MessageSQL.thread_id)};
            String selection = MessageSQL.boxid+"= ?";
            String[] selectionArgs1 = {String.valueOf(MessageSQL.MESSAGE_TYPE_FAILED)};
            Cursor cursor = getContext().getContentResolver().query(uri, projection, selection, selectionArgs1, null);
            
            
            while(cursor.moveToNext()){
                int threadId = cursor.getInt(0);
                threadIdList.add(String.valueOf(threadId));
                parameters.add("?");
            }
            cursor.close();
            
            
            Uri threadUri = QThread.THREAD_URI;
            ContentResolver mContentResolver = getContext().getContentResolver();
            ContentValues values = new ContentValues();
            values.put(QThread.error, 0);
            String where = QThread._ID+" not in ("+TextUtils.join(",", parameters)+")";
            String[] selectionArgs2= threadIdList.toArray(new String[threadIdList.size()]);
            mContentResolver.update(threadUri, values, where, selectionArgs2);
    

    此处的解决方法是先获取参数,根据参数的个数设置“?”,
    String where = QThread._ID+" not in ("+TextUtils.join(",", parameters)+")";,然后在update方法中将
    参数数组selectionArgs传入,效果相当于sql中的
    select <YOUR SELECTIONS> from <YOUR TABLE NAME> where *** in/not in (arg1,arg2,....)

    相关文章

      网友评论

          本文标题:android 日常(十九)

          本文链接:https://www.haomeiwen.com/subject/eccnvttx.html