我在向数据库添加数据时遇到了麻烦,这是一个很奇怪的问题
当我添加只有 2 个参数的数据时,如下所示
for(int i=0; i<_birthdateincontects.count; i++){
sqlite3_stmt *stmt;
int x;
char *update = "insert into PersonNamesAndBirthDates (Names,Birthdates) values(?,?);";
x = sqlite3_prepare_v2(database1, update, -1, &stmt, nil);
if (x == SQLITE_OK)
{
NSLog(@"%@",[NSString stringWithFormat:@"%@",[_namesincontects objectAtIndex:i]]);
NSLog(@"%@",[NSString stringWithFormat:@"%@",[_birthdateincontects objectAtIndex:i]]);
sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:@"%@",[_namesincontects objectAtIndex:i]] UTF8String],-1, NULL);
sqlite3_bind_text(stmt, 2, [[NSString stringWithFormat:@"%@",[_birthdateincontects objectAtIndex:i]] UTF8String],-1, NULL);
if (sqlite3_step(stmt) != SQLITE_DONE){}
sqlite3_finalize(stmt);
}
然后它完美地工作并且添加循环发生3次并且数据被添加
但是当我尝试像这样添加时
for(int i=0; i<_birthdateincontects.count; i++){
sqlite3_stmt *stmt;
int x;
char *update = "insert into PersonNamesAndBirthDates (Names,Birthdates,Profilepic) values(?,?,?);";
x = sqlite3_prepare_v2(database1, update, -1, &stmt, nil);
if (x == SQLITE_OK)
{
NSLog(@"%@",[NSString stringWithFormat:@"%@",[_namesincontects objectAtIndex:i]]);
NSLog(@"%@",[NSString stringWithFormat:@"%@",[_birthdateincontects objectAtIndex:i]]);
NSLog(@"%@",[NSString stringWithFormat:@"%@",@"No Image"]);
sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:@"%@",[_namesincontects objectAtIndex:i]] UTF8String],-1, NULL);
sqlite3_bind_text(stmt, 2, [[NSString stringWithFormat:@"%@",[_birthdateincontects objectAtIndex:i]] UTF8String],-1, NULL);
sqlite3_bind_text(stmt, 3, [[NSString stringWithFormat:@"%@",@"No Image"] UTF8String],-1, NULL);
if (sqlite3_step(stmt) != SQLITE_DONE){}
sqlite3_finalize(stmt);
}
然后与上面的循环相同,运行 3 次,但在数据库中只添加了第一个原始数据,其他时间循环运行,但没有添加任何内容,为什么会发生这种情况是我的想法。请提出一些建议,让我摆脱这种混乱
注意:nslog 打印值完美