查询ContactsContract数据库,将空字段与空字符串进行比较 - Android

时间:2014-02-05 17:34:18

标签: android sqlite android-contentprovider android-contacts contactscontract

我正在创建编辑联系人字段的方法。 在编辑之前,我确保这些字段确实存在并且与我要编辑的公司相对应,因此我进行选择并查找if(cursor.moveToFirst())但由于某种原因它不在if内我不明白。我只知道问题出在我的空Strings上,好像我删除了它们,代码运行正常。

我已下载手机联系人数据库,因此我确切知道联系人数据库中的内容。这就是为什么我要对变量进行硬编码。

这是我的代码:

public Boolean editRawContactOrganization(String rawContactId, String oldCompany, String oldType, String oldLabel, String oldTitle, 
        String oldDepartment, String oldJobDescription, String oldSymbol, String oldPhoneticName, String oldLocation,
        String newCompany, String newType, String newLabel, String newTitle, String newDepartment, String newJobDescription, 
        String newSymbol, String newPhoneticName, String newOfficeLocation)
{
    final ContentValues cv = new ContentValues();

    final String filter = Organization.RAW_CONTACT_ID + "=? AND " 
            + Organization.COMPANY + "=? AND "
            + Organization.TYPE_WORK + "=? AND "
            + Organization.LABEL + "=? AND "
            + Organization.TITLE + "=? AND "
            + Organization.DEPARTMENT + "=? AND "
            + Organization.JOB_DESCRIPTION + "=? AND "
            + Organization.SYMBOL + "=? AND "
            + Organization.PHONETIC_NAME + "=? AND "
            + Organization.OFFICE_LOCATION + "=? AND "
            + Data.MIMETYPE + "=?";

    oldCompany = "Tone Inc.";
    oldType = "2";
    oldLabel = "";
    oldTitle = "The Big Boss";
    oldDepartment = "";
    oldJobDescription = "";
    oldSymbol = "";
    oldPhoneticName = "";
    oldLocation = "";


    final String[] selectionArgs = new String[] {rawContactId, oldCompany, oldType, oldLabel, oldTitle, oldDepartment, oldJobDescription,
            oldSymbol, oldPhoneticName, oldLocation, Organization.CONTENT_ITEM_TYPE};

    final String[] projection = new String[] { Organization._ID };

    final Cursor cursor = contentResolver.query(Data.CONTENT_URI, 
            projection, 
            filter, 
            selectionArgs, 
            null);

    if(cursor.moveToFirst())
    {
        Log.d(TAG, "SUCCESS: Organization found! ID: " + cursor.getString(0));
        cursor.close();
    }
    else
    {
        Log.d(TAG, "ERROR: Organization not found...");
        cursor.close();
        return false;
    }

    return false;
}

正如您所看到的,这些变量是使用我刚从手机上下载的联系人数据库中看到的相应值进行硬编码的。

oldCompany = "Tone Inc.";
oldType = "2";
oldLabel = "";
oldTitle = "The Big Boss";
oldDepartment = "";
oldJobDescription = "";
oldSymbol = "";
oldPhoneticName = "";
oldLocation = "";

但我总是在ERROR: Organization not found...上获得logcat输出 任何人都可以指出错误在哪里?

这是一个工作示例,如果我取消注释注释代码,我会得到未找到的输出(与上面相同的问题)。与上面相同的模式,注释列内部没有值。即使我发送和清空字符串为什么不匹配空为空?

public Boolean editRawContactWebsite(String rawContactId, String oldWebsite, String oldType, String oldLabel, 
        String newWebsite, String newType, String newLabel)
{
    final ContentValues cv = new ContentValues();

    final String filter = Website.RAW_CONTACT_ID + "=? AND " 
            + Website.URL + "=? AND "
            + Website.TYPE + "=? AND "
            //+ Website.LABEL + "=? AND "
            + Data.MIMETYPE + "=?";

    oldWebsite = "www.sitr.com";
    oldType = "7";
    oldLabel = "";


    final String[] selectionArgs = new String[] {rawContactId, oldWebsite, oldType, /*oldLabel,*/ Website.CONTENT_ITEM_TYPE};

    final String[] projection = new String[] { Website._ID };

    final Cursor cursor = contentResolver.query(Data.CONTENT_URI, 
            projection, 
            filter, 
            selectionArgs, 
            null);

    if(cursor.moveToFirst())
    {
        Log.d(TAG, "SUCCESS: website found! ID: " + cursor.getString(0));
        cursor.close();
    }
    else
    {
        Log.d(TAG, "ERROR: website not found...");
        cursor.close();
        return false;
    }
    return false;
}

1 个答案:

答案 0 :(得分:0)

我通过在联系人数据库中传递与我想要编辑的行相对应的ID来修复此问题。