查询 ContactsContract 数据库,比较空字段和空字符串 - Android
Posted
技术标签:
【中文标题】查询 ContactsContract 数据库,比较空字段和空字符串 - Android【英文标题】:Query to the ContactsContract database comparing empty fields with empty strings - Android 【发布时间】:2014-03-02 07:16:58 【问题描述】:我正在创建编辑联系人字段的方法。
在编辑之前,我确保这些字段确实存在并且与我要编辑的公司相对应,所以我选择并查找 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 = "";
但我总是在logcat
上得到ERROR: Organization not found...
输出
谁能指出错误在哪里?
这是一个工作示例,如果我取消注释注释的代码,我会得到未找到的输出(与上述相同的问题)。与上面的模式相同,注释列内部没有值。即使我发送空字符串,为什么空不匹配?
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】:我通过传递与我要在联系人数据库中编辑的行相对应的 ID 解决了这个问题。
【讨论】:
以上是关于查询 ContactsContract 数据库,比较空字段和空字符串 - Android的主要内容,如果未能解决你的问题,请参考以下文章
从 Android ContactsContract.Contacts 查询所有联系人的许多数据时如何提高性能?
如何使用 CommonDataKinds.Phone 内部查询使此 ContactsContract.Contact 查询更快?
ContactsContract.Groups.SUMMARY_WITH_PHONES导致崩溃
从 ContactsContract 表中获取数据时出现 SQLITE 异常