提及未接来电名称的Android sqlite异常

时间:2014-01-23 12:25:04

标签: java android sqlite

我正在编写API以根据名称获取未接来电。在设备中有一个未接来电,其中有联系人名称Anuja.Which我想以编程方式获取。但似乎我在该名称上获得例外。在获取所有未接来电时,我得到Anuja inlist但是在提到名字时查询它不起作用。 YY?

代码:

public void missedCalls(String contactName){ 

contactName="Anuja"
String strSelection=null;

 if(contactName!=null&&!contactName.isEmpty()){
   strSelection=android.provider.CallLog.Calls.TYPE + " = "+ android.provider.CallLog.Calls.MISSED_TYPE+" AND "
                         +android.provider.CallLog.Calls.CACHED_NAME+"  =  "+contactName;
      }  
  else{

          strSelection = android.provider.CallLog.Calls.TYPE + " = "+ android.provider.CallLog.Calls.MISSED_TYPE;
      }
    Cursor missedCursor = mContext.getContentResolver().query(Calls.CONTENT_URI, null,strSelection, null, Calls.DATE + " DESC");

    }

我得到一个例外,因为android.database.sqlite.SQLiteException:没有这样的列:Anuja

2 个答案:

答案 0 :(得分:0)

我认为这会对你有帮助,

final String[] projection = null;
final String selection = null;
final String[] selectionArgs = null;
final String sortOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor cursor = null;
try{
    cursor = context.getContentResolver().query(
            Uri.parse("content://call_log/calls"),
            projection,
            selection,
            selectionArgs,
            sortOrder);
    while (cursor.moveToNext()) { 
        String callLogID = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls._ID));
        String callNumber = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
        String callDate = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.DATE));
        String callType = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.TYPE));
        String isCallNew = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.NEW));

            // The cached name associated with the phone number, if it exists. 
            // This value is not guaranteed to be current, 
            // if the contact information associated with this number has changed.

            String callerName = cursor.getString(cursor.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
            if (callerName!=null && !callerName.equalsIgnoreCase("")) {
                System.out.println("callerName: "+ callerName);
            }

        if(Integer.parseInt(callType) == MISSED_CALL_TYPE && Integer.parseInt(isCallNew) > 0){
            if (_debug) Log.v("Missed Call Found: " + callNumber);
        }
    }
}catch(Exception ex){
    if (_debug) Log.e("ERROR: " + ex.toString());
}finally{
    cursor.close();
}

答案 1 :(得分:0)

strSelection=android.provider.CallLog.Calls.TYPE + " = "+ android.provider.CallLog.Calls.MISSED_TYPE+" AND "
                     +android.provider.CallLog.Calls.CACHED_NAME+"  =  "+contactName;

contactName未引用为SQL 'literal',它被视为列名称标识符。

您可以使用DatabaseUtils.sqlEscapeString()将字符串文字换成引号并转义非安全字符:

strSelection=android.provider.CallLog.Calls.TYPE + " = "+ android.provider.CallLog.Calls.MISSED_TYPE+" AND "
                     +android.provider.CallLog.Calls.CACHED_NAME+"  =  "
                     +DatabaseUtils.sqlEscapeString(contactName);
相关问题