1

当我尝试更新 ma 表时出现异常:

这是导致崩溃的行:

rowsAffected = mDatabase.update(ApplicationDatabase.CHANNELS_TABLE_NAME, values, Channel.DATABASE_KEY_EPG_ID + " = ?", new String[] { values.getAsString(Channel.DATABASE_KEY_EPG_ID) });

这是我创建表的方式:

private static final String CHANNELS_TABLE_CREATE =
            "CREATE TABLE " + CHANNELS_TABLE_NAME + " (" +
            BaseColumns._ID + " INTEGER NOT NULL PRIMARY KEY, " +
            Channel.DATABASE_KEY_KIND + " TEXT, " +
            Channel.DATABASE_KEY_INDEX + " INTEGER, " +
            Channel.DATABASE_KEY_NAME + " TEXT, " +
            Channel.DATABASE_KEY_EPG_ID + " TEXT, " +
            Channel.DATABASE_KEY_HASHTAG + " TEXT, " +
            Channel.DATABASE_KEY_IMAGE_URL + " TEXT, " +
            Channel.DATABASE_KEY_LOGO_IMAGE_URL + " TEXT, " +
            Channel.DATABASE_KEY_LIVE_THUMBNAILS + " INTEGER, " +
            Channel.DATABASE_KEY_BLENDED_TV + " INTEGER, " +
            Channel.DATABASE_KEY_FAVORITE + " INTEGER);";

日志猫:

04-05 10:45:27.348: E/AndroidRuntime(18041): FATAL EXCEPTION: Thread-51422
04-05 10:45:27.348: E/AndroidRuntime(18041): android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:883)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1565)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1514)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at com.orange.programmetv.fr.providers.ChannelsProvider.update(ChannelsProvider.java:109)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at android.content.ContentProvider$Transport.update(ContentProvider.java:235)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at android.content.ContentResolver.update(ContentResolver.java:992)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at com.orange.programmetv.fr.services.ApplicationService.parseChannel(ApplicationService.java:354)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at com.orange.programmetv.fr.services.ApplicationService.parseChannels(ApplicationService.java:339)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at com.orange.programmetv.fr.services.ApplicationService.access$5(ApplicationService.java:327)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at com.orange.programmetv.fr.services.ApplicationService$1.work(ApplicationService.java:321)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at com.orange.programmetv.fr.services.ApplicationService$Request.run(ApplicationService.java:240)
04-05 10:45:27.348: E/AndroidRuntime(18041):    at java.lang.Thread.run(Thread.java:856)

我不明白为什么我得到 PRIMARY KEY 必须是唯一的例外?我不是在做插入,而是在做更新。

4

1 回答 1

2

在您的更新中,您将(在values_ID 中)设置为已被另一行使用的值。

不要更新值中的 _ID。

编辑

或者,您的 where 子句匹配多于 1 行并且值包含 _ID。发生子句冲突是因为您将相同的 _id 设置为几行,这是主键的性质所禁止的。

于 2013-04-05T10:09:00.283 回答