3

我尝试从特定日历中读出事件。以下是我尝试使用的代码。但我不知道选择的外观如何。我想使用日历 ID 进行选择。如果没有选择(空),它会读出所有日历,但我只想要一个。查询如下所示:

public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) 

selection:
声明要返回哪些行的过滤器,格式化为 SQL WHERE 子句(不包括 WHERE 本身)。传递 null 将返回给定 URI 的所有行。

此代码是第一次尝试选择:

            Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
            ContentUris.appendId(builder, now - DateUtils.DAY_IN_MILLIS);
            ContentUris.appendId(builder, now + DateUtils.DAY_IN_MILLIS);

            Cursor mCursor = null;
            final String[] projection = new String[]
            { CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND};

            mCursor = getContentResolver().query(
                builder.build(), projection, Instances._ID  + calendarID, null, null);
                mCursor.moveToFirst();  

那就是上面代码的错误:

09-29 00:07:41.800: E/AndroidRuntime(14507): FATAL EXCEPTION: TweetCollectorTimer
09-29 00:07:41.800: E/AndroidRuntime(14507): android.database.sqlite.SQLiteException: no such column: _id3: , while compiling: SELECT title, dtstart, dtend FROM Instances INNER JOIN view_events AS Events ON (Instances.event_id=Events._id) WHERE (begin<=? AND end>=?) AND (_id3)
09-29 00:07:41.800: E/AndroidRuntime(14507):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
09-29 00:07:41.800: E/AndroidRuntime(14507):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
09-29 00:07:41.800: E/AndroidRuntime(14507):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
09-29 00:07:41.800: E/AndroidRuntime(14507):    at android.content.ContentResolver.query(ContentResolver.java:311)
09-29 00:07:41.800: E/AndroidRuntime(14507):    at de.ring.AppService$1.run(AppService.java:96)
09-29 00:07:41.800: E/AndroidRuntime(14507):    at java.util.Timer$TimerImpl.run(Timer.java:284)

编辑:

非常感谢,我改成:

            mCursor =   getContentResolver().query(
                    builder.build(), projection, Instances.CALENDAR_ID  + " = ?",
                    new String[]{""+calendarID}, null);

使用 CALENDAR_ID 而不仅仅是 _ID 很重要。

4

1 回答 1

2

做这个

  getContentResolver().query(
                    builder.build(), projection, Instances._ID  + "=?",
new String[]{""+calendarID}, null);

而不是这个

getContentResolver().query(
                builder.build(), projection,
 Instances._ID  + calendarID, null, null);

由于第三个参数采用选择字符串,并且您将其传递给Instances._ID + calendarID它,因此它将被评估为

SELECT ........ WHERE _id3; 

它将日历 ID 的编号附加到列名。

于 2012-09-29T08:34:46.997 回答