Android Sqlite IN, NOT IN syntax

前端 未结 3 473
臣服心动
臣服心动 2021-02-05 06:54

I\'m trying to run NOT IN select where NOT IN list is dynamic. Something like

SELECT id, type FROM CONTACTS where type NOT IN (\'connec         


        
3条回答
  •  逝去的感伤
    2021-02-05 07:10

    You can have it dynamically use "not in" if you dynamically create a String that contains the list of "not in" values and then append them to the end of your sql string like in the following:

    public static Vector selectAllFormTypesForAccountIgnoringFormVersions(
            int accountId, String formsIgnored) {
    protected final static String selectAllFormTypesForAccountIgnoringFormVersions = "SELECT DISTINCT FormType.*"
            + " FROM FormType, Form WHERE Form.accountId=? AND FormType.formTypeContentId = Form.formTypeContentId"
            + " AND Form.formVersionId NOT IN (";
    
        Vector allFormTypes = new Vector();
        SQLiteDatabase db = null;
        String[] selectionArgs = { Integer.toString(accountId)};
    
        try {
            DataManager_Platform dataManager = (DataManager_Platform) DataManager_Platform
                    .getDataManager();
            db = (SQLiteDatabase) dataManager.getDatabase();
            Cursor cursor = db.rawQuery(
                    selectAllFormTypesForAccountIgnoringFormVersions + formsIgnored + ")",
                    selectionArgs);
    
            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                while (!cursor.isAfterLast()) {
                    FormType_Platform formType = new FormType_Platform();
                    formType.populateModel(cursor);
                    allFormTypes.add(formType);
                    cursor.moveToNext();
                }
            }
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (db != null) {
                    db.close();
                }
            } catch (Exception e) {
                System.out.println("Error: " + e.getMessage());
                e.printStackTrace();
            }
        }
    
        return allFormTypes;
    }
    

提交回复
热议问题