当我尝试使用复合主键保存在表中时,当同一Event.Game id
与同一private static final String CREATE_TB_EVENT = "CREATE TABLE IF NOT EXISTS "+TABLE_EVENT+" ( GAME INTEGER NOT NULL, " +
"PLAYER INTEGER NOT NULL, TYPE INTEGER NOT NULL, MINUTE INTEGER NOT NULL, PRIMARY KEY ( GAME, PLAYER, TYPE, MINUTE ), " +
"FOREIGN KEY ( GAME ) REFERENCES "+TABLE_GAME+" ( IDGAME ), " +
"FOREIGN KEY ( PLAYER ) REFERENCES "+TABLE_PLAYER+" ( IDPLAYER ) )";
一起使用时,我得到错误UNIQUE约束失败,但因为它是4-复合主键不应该给出问题。
创建表格代码:
TABLE_GAME
首次更新IDGAME
中的记录(未触及TABLE_EVENT
)时会发生错误,然后我删除IDGAME
中
final String DELETE_EVENTS = "DELETE FROM "+TABLE_EVENT+" WHERE GAME = "+game.getId();
db.execSQL(DELETE_EVENTS);
的所有行:
public boolean saveEvents(ArrayList<Event> events, int gameID) {
if(events == null)
return false;
try {
SQLiteDatabase db = getWritableDatabase();
Log.v(TAG, getTableAsString(db, TABLE_EVENT));
ContentValues cv = new ContentValues();
for(Event e : events) {
cv.put("GAME",gameID);
cv.put("PLAYER",e.getIdPlayer());
cv.put("TYPE",e.getType());
cv.put("MINUTE",e.getMinute());
db.insert(TABLE_EVENT, null, cv);
Log.v(TAG, getTableAsString(db, TABLE_EVENT));
}
db.close();
return true;
} catch (SQLiteException | NullPointerException ex) {
Log.e(TAG, ex.getMessage());
return false;
}
}
然后我调用这种方法来获取违反的唯一约束
Error inserting MINUTE=90 PLAYER=4 TYPE=2 GAME=54
UNIQUE constraint failed: Event.GAME, Event.PLAYER (code 1555)
错误:TABLE_EVENT
当我登录上面的方法打印Table Event: empty
Table Event: GAME: 54 PLAYER: 4 TYPE: 1 MINUTE: 23
Table Event: GAME: 54 PLAYER: 4 TYPE: 1 MINUTE: 23
GAME: 54 PLAYER: 5 TYPE: 2 MINUTE: 45
Table Event: GAME: 54 PLAYER: 4 TYPE: 1 MINUTE: 23
GAME: 54 PLAYER: 5 TYPE: 2 MINUTE: 45
GAME: 54 PLAYER: 10 TYPE: 1 MINUTE: 23
Table Event: GAME: 54 PLAYER: 4 TYPE: 1 MINUTE: 23
GAME: 54 PLAYER: 5 TYPE: 2 MINUTE: 45
GAME: 54 PLAYER: 10 TYPE: 1 MINUTE: 23
时,我得到:
GAME=54 PLAYER=4 TYPE=2 MINUTE=90
如您所见,最后两个输出是相同的,因为它没有用
添加新行 JobInfo.Builder jobInfoBuilder = new JobInfo.Builder(JobServicesIds.CONNECTIVITY.getValue(), new ComponentName(context.getPackageName(), ConnectivityJobService.class.getName()));
jobInfoBuilder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NOT_ROAMING);
jobInfoBuilder.setPersisted(false);
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
int result = jobScheduler.schedule(jobInfoBuilder.build());
我试过SQL Fiddle,一切正常,
我错过了什么?
答案 0 :(得分:0)
我解决了这个问题。由于Google的自动备份,我的表格现有副本,这就是为什么它一直说UNIQUE constraint failed
。
解决方案进入Android Manifest
并设置
allowBackup = "false"