Skip to content Skip to sidebar Skip to footer

Sqlite Constraint Exception Primary Key Must Be Unique

I have 3 tables Table 1 : Member - To store every information about the member, Attribute : MemberUsername(PK), MemberPassword Table 2 : MsGroup - To store every registered group A

Solution 1:

The problem is your GROUP_ID is not unique. If you have two members in group 1, your table will have two entries --

1, Member 1
1, Member 2

By specifying "PRIMARY KEY" on GROUP_ID you are stating it will be unique. You need different primary key. In android, it is common to use "_id", something like --

db.execSQL("CREATE TABLE IF NOT EXISTS " + MS_GROUP_DETAIL + " ("
                + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + GROUP_ID + " INTEGER, " + MEMBER_USERNAME
                + " TEXT,  FOREIGN KEY (" + MEMBER_USERNAME
                + ") REFERENCES " + MS_MEMBER + "(" + MEMBER_USERNAME
                + "), FOREIGN KEY (" + GROUP_ID + ") REFERENCES "
                + MS_GROUP + "(" + GROUP_ID + "));");

Post a Comment for "Sqlite Constraint Exception Primary Key Must Be Unique"