Note that there are some explanatory texts on larger screens.

plurals
  1. PO'INSERT INTO' suddenly give back error 'PRIMARY KEY must be unique'
    text
    copied!<p>I've an sqlite database on my iOS app. I found out I'd a mistake with the columns' numbers, then I changed to the correct one and since then I get an error 'PRIMARY KEY must be unique'. Before the mistake all went well (beside 2 columns that misplaced values).</p> <p>My code:</p> <pre><code>- (BOOL)createGroupMemberTable { char *error = NULL; const char *sqlStatment = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' (ID INTEGER PRIMARY KEY AUTOINCREMENT, %@ INTEGER, %@ INTEGER, '%@' TEXT, '%@' TEXT, '%@' TEXT, '%@' TEXT, '%@' TEXT, '%@' TEXT, '%@' TEXT, '%@' TEXT);", kTableNameGroupMember, kMemberID, kGroupID, kName, kTime, kLatitude, kLongitude, kMobile, kVisible, kAddress, kPicture].UTF8String; int responseCode = sqlite3_exec(_database, sqlStatment, NULL, NULL, &amp;error); if (responseCode != SQLITE_OK) { sqlite3_close(_database); printf("Table 'GroupMember' failed to create, responseCode: %i\n", responseCode); return NO; } else { return YES; } } - (BOOL)addGroupMember:(GroupMember*)aGroupMember { char *error = NULL; const char *sqlStatment = [NSString stringWithFormat:@"INSERT INTO %@ ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@') VALUES (%i, %i, '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@');", kTableNameGroupMember, kMemberID, kGroupID, kName, kTime, kLatitude, kLongitude, kMobile, kVisible, kAddress, kPicture, aGroupMember.memberID, aGroupMember.groupID, aGroupMember.name, aGroupMember.time, aGroupMember.latitude, aGroupMember.longitude, aGroupMember.mobile, aGroupMember.visible, aGroupMember.address, aGroupMember.picture].UTF8String; int responseCode = sqlite3_exec(_database, sqlStatment, NULL, NULL, &amp;error); if (responseCode != SQLITE_OK) { printf("\n\nFailed to insert into 'GroupMember' table, responseCode: %i\n", responseCode); printf("Error Message: %s\n\n", sqlite3_errmsg(_database)); return NO; } else { return YES; } } </code></pre> <p>It might be irrelevant but this is my code to db creation:</p> <pre><code>static SQLiteHandler *_database; + (SQLiteHandler*)database { if (_database == nil) { _database = [[SQLiteHandler alloc] init]; } return _database; } - (NSString*)databasePath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDirectory = [paths objectAtIndex:0]; return [docDirectory stringByAppendingPathComponent:@"social.sqlite3"]; } - (id)init { if ((self = [super init])) { const char *fileName = [self databasePath].UTF8String; if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%s", fileName]]) { printf("SQLITE EXISTS\n"); } else { printf("SQLITE NOT EXISTS\n"); const char *finalPath = [self databasePath].UTF8String; printf("File 'social.sqlite3' created at path: %s\n", finalPath); } printf("fileName: %s\n", fileName); int responseCode = sqlite3_open(fileName, &amp;_database); if (responseCode != SQLITE_OK) { printf("Failed to open database, responseCode: %i\n", responseCode); } [self createGroupTable]; [self createGroupMemberTable]; } return self; } </code></pre> <p>Error log:</p> <blockquote> <p>Failed to insert into 'GroupMember' table, responseCode: 19 Error Message: PRIMARY KEY must be unique</p> <h1>define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */</h1> </blockquote> <p>Any ideas?</p> <p><strong>EDIT:</strong></p> <p>Answer was as I thought eventually as commented here by 'Petesh'. All answers are correct so I'll mark the 1st one.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload