我在向数据库添加数据时遇到了麻烦,这是一个非常奇怪的问题
当我添加只有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次,但在数据库中只有第一个原始添加和其他时间循环运行但没有添加为什么这发生的方式是我的方式。 PLZ 提出一些建议,让我摆脱这种混乱
注意:nslog打印值完美
答案 0 :(得分:1)
通常,当SQL语句失败时,您可以查看sqlite3_errmsg
,它会告诉您出了什么问题,例如:
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, [@"No Image" UTF8String],-1, NULL);
if ((x = sqlite3_step(stmt)) != SQLITE_DONE) {
NSLog(@"%s: step failed: %s (%d)", __FUNCTION__, sqlite3_errmsg(database1), x);
}
sqlite3_finalize(stmt);
}
else
{
NSLog(@"%s: prepare failed: %s (%d)", __FUNCTION__, sqlite3_errmsg(database1), x);
}
}
顺便说一句,你也可以通过不每次重新准备SQL来提高效率,例如:
char *update = "insert into PersonNamesAndBirthDates (Names,Birthdates,Profilepic) values(?,?,?);";
sqlite3_stmt *stmt;
int x;
if ((x = sqlite3_prepare_v2(database1, update, -1, &stmt, nil)) != SQLITE_OK)
{
NSLog(@"%s: prepare failed: %s (%d)", __FUNCTION__, sqlite3_errmsg(database1), x);
return;
}
for (int i = 0; i < _birthdateincontects.count; 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);
sqlite3_bind_text(stmt, 3, "No Image", -1, NULL);
if ((x = sqlite3_step(stmt)) != SQLITE_DONE) {
NSLog(@"%s: step failed: %s (%d)", __FUNCTION__, sqlite3_errmsg(database1), x);
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);