PRIMARY KEY在呼叫更新时必须是唯一的例外

时间:2013-04-05 09:54:49

标签: android sqlite

我在尝试更新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);";

logcat:

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必须是唯一的例外?我没有插入我正在进行更新。

1 个答案:

答案 0 :(得分:2)

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

请勿更新数值中的_ID。

修改

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