SQLite:独特的插入问题
SQLite: Unique insert issue
我在使用指定的复合主键将行插入 table 时遇到问题。
复合键:
_id = id of the series (this starts always with 0 for every new shooting)
shooting_id = id of my training session
program_id = id of discipline I am currently training
每当我尝试插入具有相同“_id”列值的第二行时,我都会收到此错误:
INSERT INTO series(_id,shooting_id,rings,program_id) VALUES (?,?,?,?)]: UNIQUE constraint failed: series._id, series._id, series._id 05-19 08:36:05.070 2417-2417/? E/SQLiteDatabase﹕ Error inserting _id=0 shooting_id=1 rings=100 program_id=0 android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: series._id, series.program_id, series._id (code 2067)
示例:
// first training session
INSERT INTO series(_id,shooting_id,rings,program_id) VALUES (0,0,100,0)
INSERT INTO series(_id,shooting_id,rings,program_id) VALUES (1,0,98,0)
INSERT INTO series(_id,shooting_id,rings,program_id) VALUES (2,0,98,0)
// second training session --> only shooting_id has changed (!!)
INSERT INTO series(_id,shooting_id,rings,program_id) VALUES (0,1,100,0) <-- results in above error
table 是这样指定的:
private static final String CREATE_TABLE_SERIES = "CREATE TABLE IF NOT EXISTS "
+ TABLE_SERIES + " ("
+ COLUMN_SERIES_SHOOTING_ID + " integer NOT NULL, "
+ COLUMN_SERIES_PROGRAM_ID + " integer NOT NULL, "
+ COLUMN_SERIES_ID + " integer NOT NULL, "
+ COLUMN_SERIES_RINGS + " integer NOT NULL, "
+ "FOREIGN KEY(" + COLUMN_SERIES_SHOOTING_ID + ") REFERENCES " + TABLE_SHOOTING + "(_id), "
+ "UNIQUE (" + COLUMN_SHOOTING_ID + "," + COLUMN_SERIES_PROGRAM_ID + "," + COLUMN_SERIES_ID + "));";`
为什么?? :-(
您的 UNIQUE
约束是错误的。列名称变量具有来自其他 table 的 _id
,而不是 series
table 中的列,并且约束变为 (_id,_id,_id)
,而不是 (_id,shooting_id,program_id)
.
我在使用指定的复合主键将行插入 table 时遇到问题。
复合键:
_id = id of the series (this starts always with 0 for every new shooting)
shooting_id = id of my training session
program_id = id of discipline I am currently training
每当我尝试插入具有相同“_id”列值的第二行时,我都会收到此错误:
INSERT INTO series(_id,shooting_id,rings,program_id) VALUES (?,?,?,?)]: UNIQUE constraint failed: series._id, series._id, series._id 05-19 08:36:05.070 2417-2417/? E/SQLiteDatabase﹕ Error inserting _id=0 shooting_id=1 rings=100 program_id=0 android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: series._id, series.program_id, series._id (code 2067)
示例:
// first training session
INSERT INTO series(_id,shooting_id,rings,program_id) VALUES (0,0,100,0)
INSERT INTO series(_id,shooting_id,rings,program_id) VALUES (1,0,98,0)
INSERT INTO series(_id,shooting_id,rings,program_id) VALUES (2,0,98,0)
// second training session --> only shooting_id has changed (!!)
INSERT INTO series(_id,shooting_id,rings,program_id) VALUES (0,1,100,0) <-- results in above error
table 是这样指定的:
private static final String CREATE_TABLE_SERIES = "CREATE TABLE IF NOT EXISTS "
+ TABLE_SERIES + " ("
+ COLUMN_SERIES_SHOOTING_ID + " integer NOT NULL, "
+ COLUMN_SERIES_PROGRAM_ID + " integer NOT NULL, "
+ COLUMN_SERIES_ID + " integer NOT NULL, "
+ COLUMN_SERIES_RINGS + " integer NOT NULL, "
+ "FOREIGN KEY(" + COLUMN_SERIES_SHOOTING_ID + ") REFERENCES " + TABLE_SHOOTING + "(_id), "
+ "UNIQUE (" + COLUMN_SHOOTING_ID + "," + COLUMN_SERIES_PROGRAM_ID + "," + COLUMN_SERIES_ID + "));";`
为什么?? :-(
您的 UNIQUE
约束是错误的。列名称变量具有来自其他 table 的 _id
,而不是 series
table 中的列,并且约束变为 (_id,_id,_id)
,而不是 (_id,shooting_id,program_id)
.