user2189388
user2189388

Reputation: 202

database insertion issue

i am getting trouble in adding data to database and this is so weird problem

when i add data with only 2 arguments like below

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);


    }

then it works perfectly and add loop happen 3 times and data gets add

but when i try to add like this

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);


    }

then same like above loop runs 3 times but in database only 1st raw get added and other time loop run but nothing get added why this is happening is way of my head. plz suggest something and make me out of this discombobulation

NOTE: nslog print value perfectly

Upvotes: 0

Views: 95

Answers (1)

Rob
Rob

Reputation: 437432

Often when SQL statements fail, you can look at sqlite3_errmsg and it will tell you what's gone wrong, e.g.:

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);
    }
}

By the way, you can also make this more efficient by not re-preparing the SQL every time, e.g.:

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);

Upvotes: 1

Related Questions