0

我在使用 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; 
}

关于我哪里出错的任何帮助?

4

1 回答 1

0

您似乎错误地使用了选择参数 - 您的选择中应该有 a ?,并且它们?被替换为选择参数中的值,如果需要,自动将它们用引号引起来:

String selection = 
    CalendarContract.Calendars.ACCOUNT_NAME + 
    " =? AND " + 
    CalendarContract.Calendars.ACCOUNT_TYPE + 
    " =?"; 
String[] selArgs = 
    new String[]{
        accountName, 
        CalendarContract.ACCOUNT_TYPE_LOCAL
}; 
于 2014-07-03T17:11:37.453 回答