【问题标题】:SQLite IntegrityError: UNIQUE constraint failed:SQLite IntegrityError:唯一约束失败:
【发布时间】:2015-03-13 16:52:27
【问题描述】:

我刚刚收到一个奇怪的错误:

IntegrityError: UNIQUE constraint failed: jumptimes.player_id, jumptimes.map_id, jumptimes.runID

我的 SQL 查询:

t = (playerid, mapid, timeTaken, (time() if not dateOverwrite else dateOverwrite), runID, runLeaveZoneVelocity, EnterZoneVelocity, averageVelocity, time())
log("PlayerID: %s | mapid: %s | timeTaken: %s | Date: %s | runID: %s | rlvz: %s | ezv: %s | avgvel: %s | firstFinish: %s" % t)
execute("INSERT INTO jumptimes (player_id, map_id, duration, date, runID, LeaveZoneVelocity, enterZoneVelocity, averageVelocity, firstFinish) VALUES (?,?,?,?,?,?,?,?,?)", t)

日志输出:

17:45:11 - PlayerID: 13 | mapid: 34 | timeTaken: 55.2569999695 | Date: 1426265111.18 | runID: 0 | rlvz: 315.484661963 | ezv: 1159.06484472 | avgvel: 1374.49441131 | firstFinish: 1426265111.18

我的数据库结构:

CREATE TABLE IF NOT EXISTS jumptimes (
     id INTEGER PRIMARY KEY AUTO_INCREMENT,
     player_id INTEGER REFERENCES players ON DELETE CASCADE,
     map_id INTEGER REFERENCES maps ON DELETE CASCADE,
     duration REAL,
     `date` REAL,
     runID INTEGER,
     leaveZoneVelocity INTEGER DEFAULT 0,
     enterZoneVelocity INTEGER DEFAULT 0,
     averageVelocity INTEGER DEFAULT 0,
     server INTEGER DEFAULT 0,
     firstFinish REAL,
     completions INTEGER DEFAULT 1,
     UNIQUE (player_id, map_id, runID)
)

正如主题所说,我总是得到SQLite IntegrityError: UNIQUE constraint failed:

【问题讨论】:

标签: sql sqlite python-2.5


【解决方案1】:

表声明的UNIQUE 子句指出player_idmap_idrunID 的每一行组合必须是唯一的。您收到此错误是因为jumptimes 表中已经有一行包含player_id = 13map_id = 34runID = 0

Here is a simple SQLFiddle 重现错误。错误所说的“约束”是 UNIQUE 子句,如果您能看到完整的错误,它将与您得到的相同。

【讨论】:

    猜你喜欢
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2016-07-30
    • 2020-10-10
    • 2023-03-20
    • 2023-03-29
    相关资源
    最近更新 更多