0

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

当我添加只有 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 打印值完美

4

1 回答 1

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);
于 2013-03-25T14:02:00.943 回答