SQLITE检查id是否具有特定值

时间:2018-04-09 11:58:20

标签: sqlite

我有一个像这样的SQLITE表:

--------------------------------
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

 Add this line in manifest

 if any error then 
    <permission
    android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:protectionLevel="signature" />
  - Optional 


------------------------------

public class PermissionUtils 
{

Context context;
Activity current_activity;

PermissionResultCallback permissionResultCallback;


ArrayList<String> permission_list=new ArrayList<>();
ArrayList<String> listPermissionsNeeded=new ArrayList<>();
String dialog_content="";
int req_code;

public PermissionUtils(Context context)
{
    this.context=context;
    this.current_activity= (Activity) context;

    permissionResultCallback= (PermissionResultCallback) context;
}


/**
 * Check the API Level & Permission
 *
 * @param permissions
 * @param dialog_content
 * @param request_code
 */

public void check_permission(ArrayList<String> permissions, String dialog_content, int request_code)
{
    this.permission_list=permissions;
    this.dialog_content=dialog_content;
    this.req_code=request_code;

   if(Build.VERSION.SDK_INT >= 23)
   {
       if (checkAndRequestPermissions(permissions, request_code))
       {
           permissionResultCallback.PermissionGranted(request_code);
           Log.i("all permissions", "granted");
           Log.i("proceed", "to callback");
       }
   }
    else
   {
       permissionResultCallback.PermissionGranted(request_code);

       Log.i("all permissions", "granted");
       Log.i("proceed", "to callback");
   }

}


/**
 * Check and request the Permissions
 *
 * @param permissions
 * @param request_code
 * @return
 */

private  boolean checkAndRequestPermissions(ArrayList<String> permissions, int request_code) {

    if(permissions.size()>0)
    {
        listPermissionsNeeded = new ArrayList<>();

        for(int i=0;i<permissions.size();i++)
        {
            int hasPermission = ContextCompat.checkSelfPermission(current_activity,permissions.get(i));

            if (hasPermission != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(permissions.get(i));
            }

        }

        if (!listPermissionsNeeded.isEmpty())
        {
            ActivityCompat.requestPermissions(current_activity, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),request_code);
            return false;
        }
    }

    return true;
}

/**
 *
 *
 * @param requestCode
 * @param permissions
 * @param grantResults
 */
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
{
    switch (requestCode)
    {
        case 1:
             if(grantResults.length>0)
             {
                 Map<String, Integer> perms = new HashMap<>();

                 for (int i = 0; i < permissions.length; i++)
                 {
                     perms.put(permissions[i], grantResults[i]);
                 }

                 final ArrayList<String> pending_permissions=new ArrayList<>();

                 for (int i = 0; i < listPermissionsNeeded.size(); i++)
                 {
                     if (perms.get(listPermissionsNeeded.get(i)) != PackageManager.PERMISSION_GRANTED)
                     {
                        if(ActivityCompat.shouldShowRequestPermissionRationale(current_activity,listPermissionsNeeded.get(i)))
                                pending_permissions.add(listPermissionsNeeded.get(i));
                        else
                        {
                            Log.i("Go to settings","and enable permissions");
                            permissionResultCallback.NeverAskAgain(req_code);
                           // Toast.makeText(current_activity, "Go to settings and enable permissions", Toast.LENGTH_LONG).show();
                            return;
                        }
                     }

                 }

                 if(pending_permissions.size()>0)
                 {
                     showMessageOKCancel(dialog_content,
                             new DialogInterface.OnClickListener() {
                                 @Override
                                 public void onClick(DialogInterface dialog, int which) {

                                     switch (which) {
                                         case DialogInterface.BUTTON_POSITIVE:
                                             check_permission(permission_list,dialog_content,req_code);
                                             break;
                                         case DialogInterface.BUTTON_NEGATIVE:
                                             Log.i("permisson","not fully given");
                                             if(permission_list.size()==pending_permissions.size())
                                                 permissionResultCallback.PermissionDenied(req_code);
                                             else
                                                 permissionResultCallback.PartialPermissionGranted(req_code,pending_permissions);
                                             break;
                                     }


                                 }
                             });

                 }
                 else
                 {
                    Log.i("all","permissions granted");
                    Log.i("proceed","to next step");
                     permissionResultCallback.PermissionGranted(req_code);

                 }



             }
             break;
    }
}


/**
 * Explain why the app needs permissions
 *
 * @param message
 * @param okListener
 */
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
    new AlertDialog.Builder(current_activity)
            .setMessage(message)
            .setPositiveButton("Ok", okListener)
            .setNegativeButton("Cancel", okListener)
            .create()
            .show();
   }

 }

 =============================


    // You can add any permission here

    ArrayList<String> permissions = new ArrayList<>();
    PermissionUtils permissionUtils;


    permissionUtils = new PermissionUtils(context);

    permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);

    //i.e permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);

    permissionUtils.check_permission(permissions,getResources()
   .getString(R.string.explain_here_why_the_app_needs_permissions), 1);


 =============================

 Activty that implements PermissionResultCallback

 ===================

 Make interface

 public interface PermissionResultCallback
 {
  void PermissionGranted(int request_code);
  void PartialPermissionGranted(int request_code, ArrayList<String> 
                                                granted_permissions);
  void PermissionDenied(int request_code);
  void NeverAskAgain(int request_code);
 }

-----------------------------

小提琴:click here

现在我尝试获取一个ID列表,其中键不是60或OR键是60,值= 8.当我做一个简单的:

id      key      value
244574  16       999
244574  18       999
244574  54       174
214808  16       662
214808  17       808
214808  33       1
214808  60       2
214809  16       902 
214809  17       1103
214809  33       1
214809  60       2
218965  19       808
218965  21       662
218965  33       1
218965  60       8
218966  19       1103
218966  21       902
218966  33       1
218966  60       8

我得到那些键不是60的行。但我只想得到三行:

select * from items_attributes where (key != 60) OR (key = 60 AND value = 8);

由于我是SQL的初学者,我可以想到一个带有concat的解决方案,但我没有任何线索。也许有人能够帮助我。

提前致谢!

祝你好运, 安德烈亚斯

2 个答案:

答案 0 :(得分:2)

怎么样:

select 
    id,
    '[' || group_concat(key, ';') || ']',
    '[' || group_concat(value, ';') || ']'
from items_attributes
group by id
having 
    -- no rows with key 60 for this id
    count(case when key = 60 then 1 end) = 0 
or 
    -- 1 or more rows with key 60 & value 8 for this id
    count(case when key = 60 and value = 8 then 1 end) > 0

答案 1 :(得分:0)

我不确定你想要如何只获得三行。但是,以下查询: -

SELECT 
    id, 
    '['||group_concat(key,';')||']' AS key_group, 
    '['||group_concat(value,';')||']' AS value_group
FROM items_attributes 
WHERE (key != 60) OR (key = 60 AND value = 8) 
GROUP BY id;

结果: -

enter image description here

我现在明白了。这看起来像你想要的那样: -

SELECT 
    id, 
    '['||group_concat(key,';')||']' AS key, 
    '['||group_concat(value,';')||']' AS value 
FROM items_attributes WHERE id NOT IN(
    SELECT id FROM items_attributes WHERE (key = 60 AND value != 8))
GROUP by id ORDER BY id DESC;

enter image description here