简体   繁体   中英

Android sqllite exception on mentioning missed call name

I am writing API to fetch missed call on the basis of name.There is a missed call in device which has contact name Anuja.Which I want to fetch programatically.But seems I am getting exception on that name. On fetching all missed call I get that Anuja inlist but on quering by mentioning name it does not work. yy?

Code:

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");

    }

I am getting an exception as android.database.sqlite.SQLiteException: no such column: Anuja

I think this will help you,

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();
}
strSelection=android.provider.CallLog.Calls.TYPE + " = "+ android.provider.CallLog.Calls.MISSED_TYPE+" AND "
                     +android.provider.CallLog.Calls.CACHED_NAME+"  =  "+contactName;

contactName is not quoted as a SQL 'literal' and it is treated as a column name identifier.

You can use DatabaseUtils.sqlEscapeString() with to wrap the string literal in quotes and escape non-safe characters:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM