我对 ios (Objective-C) 和 sql 非常陌生,我正在遵循此处找到的 sqlite (特别是 sqlite3) 教程:http ://www.tutorialspoint.com/ios/ios_sqlite_database.htm 我正确地实现了一切。问题是,如果我尝试使用相同的 reg id(用于在数据库中查找元素的主键)输入信息,它会抱怨该键不是唯一的。
- (BOOL) saveData:(NSString*)registerNumber name:(NSString*)name
department:(NSString*)department year:(NSString*)year;
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *insertSQL =
[NSString stringWithFormat:@"insert into studentsDetail (regno,name, department, year) values (\"%d\",\"%@\", \"%@\", \"%@\")",[registerNumber integerValue], name, department, year];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
int s = sqlite3_step(statement);
const char *errmsg = sqlite3_errmsg(database);
if (s == SQLITE_DONE)
{
sqlite3_reset(statement);
return YES;
}
else {
sqlite3_reset(statement);
return NO;
}
}
return NO;
}
如何进行 saveData: 操作实际上更新数据库中的条目,而不是仅仅被拒绝?谢谢!