Android CalendarContract帐户名称问题

时间:2014-07-03 16:55:44

标签: android calendar calendarcontract

我在使用CalendarContract API选择帐户时遇到问题。我收到以下错误:

  

引起:android.database.sqlite.SQLiteException:靠近“@iancorneli”:语法错误(代码1):,同时编译:SELECT _id FROM Calendars WHERE(account_name = me@iancorneli.us account_type = LOCAL)

代码如下:

private void getAccounts() {
    AccountManager manager = AccountManager.get(context);
    Account[] accounts = manager.getAccountsByType("com.google");

    for (Account account : accounts) {
        accountName = account.name;
        accountType = account.type;
        break;
    }
}

private long getCalendarID() {
    getAccounts();
    String[] projection = new String[]{CalendarContract.Calendars._ID}; 
    String selection = 
        CalendarContract.Calendars.ACCOUNT_NAME + 
        " = " + accountName + " " + 
        CalendarContract.Calendars.ACCOUNT_TYPE + 
        " = " + CalendarContract.ACCOUNT_TYPE_LOCAL + " "; 
    String[] selArgs = 
        new String[]{
            CalendarContract.Calendars.ACCOUNT_NAME, 
            CalendarContract.ACCOUNT_TYPE_LOCAL
    }; 
    Cursor cursor = context.getContentResolver().query(
        CalendarContract.Calendars.CONTENT_URI, 
        projection, 
        selection, 
        selArgs, 
        null
    ); 
    if (cursor.moveToFirst()) { 
        return cursor.getLong(0); 
    } 
    return -1; 
}

有关我出错的地方有任何帮助吗?

1 个答案:

答案 0 :(得分:0)

您似乎使用了选择参数错误 - 您的选择中应该有?,并且?会被选择参数中的值替换,必要时会自动将它们包装在引号中:

String selection = 
    CalendarContract.Calendars.ACCOUNT_NAME + 
    " =? AND " + 
    CalendarContract.Calendars.ACCOUNT_TYPE + 
    " =?"; 
String[] selArgs = 
    new String[]{
        accountName, 
        CalendarContract.ACCOUNT_TYPE_LOCAL
};