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回実行されますが、データベースでは最初のrawループが追加され、他の時間ループが実行されますが、これが発生する理由は何も追加されません。plzは何かを提案し、この混乱から私を解放します

注: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 に答える