我在向数据库添加数据时遇到麻烦,这是一个很奇怪的问题

当我仅使用以下两个参数添加数据时

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次,但是在数据库中仅添加了第一个原始数据,而其他时间循环运行了,但是什么也没有添加,这是为什么呢? z
提出一些建议并使我摆脱这种混乱

注意:nslog打印值完美

最佳答案

通常,当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);

关于ios - 数据库插入问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15614318/

10-12 22:22
查看更多