Ios 将记录添加到SQLite会导致添加额外的随机记录
我正在用iOS和SQLite数据库编写一个应用程序。 我有一个tableView,其中显示了研讨会列表,其中包含的信息包括:标题、演示者姓名。当用户单击“添加”按钮时,将以模式显示新视图。新视图包含一个名称列表。用户可以通过在此列表中的某些名称上单击鼠标右键来选择研讨会演示者,然后选中的名称在其旁边打勾。用户单击“保存”后,记录将正确添加到数据库中,视图将被取消。 然而,在我返回到带有研讨会列表的视图后,我发现新添加的研讨会的演示者比我实际添加的多。 当我检查数据库文件时,我发现额外的记录实际上添加到了数据库中,这意味着代码中不是语义错误导致添加额外的名称。 此外,我还调试了插入查询所在的代码。它明确表示只有选中的名称被添加到数据库中,并且我记录查询结果,它只打印出选中的名称。 我不知道这些额外的名字到底加在哪里了。我试图找到一个模式,但每次都会在数据库中添加不同的名称作为额外名称。 奇怪的是,这个问题并不总是发生 更新: 下面是我在Database.m文件中的代码片段。我假设我从我的addWorkshop视图控制器向研讨会添加了两个演示者Ios 将记录添加到SQLite会导致添加额外的随机记录,ios,sqlite,Ios,Sqlite,我正在用iOS和SQLite数据库编写一个应用程序。 我有一个tableView,其中显示了研讨会列表,其中包含的信息包括:标题、演示者姓名。当用户单击“添加”按钮时,将以模式显示新视图。新视图包含一个名称列表。用户可以通过在此列表中的某些名称上单击鼠标右键来选择研讨会演示者,然后选中的名称在其旁边打勾。用户单击“保存”后,记录将正确添加到数据库中,视图将被取消。 然而,在我返回到带有研讨会列表的视图后,我发现新添加的研讨会的演示者比我实际添加的多。 当我检查数据库文件时,我发现额外的记录实际上
-(void)addWorksopWithTitle:(NSString *)title presenters:(NSMutableArray *)presenters date:(NSDate *)date time:(NSString *)time bld:(NSString *)bld floor:(int)floor room:(NSString *)room manadatory:(BOOL)manadatory
{
[self openConnection];
const char *SqlStatement = "insert into Workshop (wid,title,manadatory,date,time,bld,floor,room,wsdate,dateD) values(null,?,?,?,?,?,?,?,?,?)";
//end of edited code
sqlite3_stmt *compileStatement1;
int success;
if (sqlite3_prepare_v2(database, SqlStatement, -1, &compileStatement1, NULL) == SQLITE_OK)
{
NSLog(@"insert WS prepair");
sqlite3_bind_text(compileStatement1, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(compileStatement1, 2, manadatory);
sqlite3_bind_text(compileStatement1, 3, [@"Feb 15, 2014" UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compileStatement1, 4, [time UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compileStatement1, 5, [bld UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(compileStatement1, 6, floor);
sqlite3_bind_text(compileStatement1, 7, [room UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_double(compileStatement1, 8, [date timeIntervalSince1970]);
sqlite3_bind_double(compileStatement1, 9, [date timeIntervalSince1970]);
success = sqlite3_step(compileStatement1);
NSLog(@"sucess insert ws msg:%d",success);
}else
{
NSLog(@"insert WS NOT success");
NSAssert1(0, @"Error: failed to prepare statement with message '%s'. ", sqlite3_errmsg(database));
}
sqlite3_finalize(compileStatement1);
if (success == SQLITE_ERROR)
{
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
}
//get the last inserted workshop id
int wid = (int) sqlite3_last_insert_rowid(database);
//NSLog statement to get the number of presenters to be added. it returns 2
NSLog(@"#presenters to add: %lu",(unsigned long)[presenters count]);
//insert presenters in Present table
for (Faculty *presenter in presenters) {
const char *SqlStatement2 = "insert into Presents(pid,wid,fid) values(null,?,?)";
sqlite3_stmt *compileStatement2;
int success2;
if (sqlite3_prepare_v2(database, SqlStatement2, -1, &compileStatement2, NULL) == SQLITE_OK)
{
sqlite3_bind_int(compileStatement2, 1, wid);
sqlite3_bind_int(compileStatement2, 2, [presenter fid]);
success2 = sqlite3_step(compileStatement2);
NSLog(@"insert to present,sucess msg:%d",success2); //success message prints 101. which according to SQLite documentation means the sqlite3_step() has finished executing
}else
{
NSLog(@"insert to present NOT success");
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
sqlite3_finalize(compileStatement2);
if (success2 == SQLITE_CONSTRAINT)
{
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
}
}
[self closeConnection];
}
此代码有时只会添加额外记录如果没有看到您所引用的代码,任何人都无法帮助您。话虽如此,请按照顺序进行操作。如果您在日志中得到正确的结果,但在数据库中保存的结果不正确,那么这两点之间一定发生了什么。我的第一个问题是,您是使用绑定查询,还是尝试构建查询字符串小Bobby表样式?因为如果是后者,你可能会因为单引号和其他特殊字符而得到多次插入。我每周都会更新我的问题。请不要看了,太客气了。