簡體   English   中英

Android sqllite異常,提及未接來電名稱

[英]Android sqllite exception on mentioning missed call name

我正在編寫API以根據名稱獲取未接來電。設備中有一個未接來電,其聯系人姓名為Anuja。我想以編程方式獲取該電話,但似乎我在該名稱上遇到了異常。 在獲取所有未接來電時,我得到了Anuja inlist,但在通過提及姓名進行查詢時不起作用。

碼:

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

我想這對您有幫助

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沒有被引述為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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM