Steve Gear
Steve Gear

Reputation: 749

How to insert NSMutableArray elements in Sqlite3 in iPhone

When I am inserting my array elements into sqlite, it is inserting properly. It takes only the first element in array, for remaining it is showing error while inserting. My code is:

NSString *path=[self getDBPath];
sqlite3_stmt *insert_statement = nil;    

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
    for (int i = 0; i < [array count]; i++) 
    {
        NSLog(@"Array Count is %d",[array count]);
        NSString *name=[[array objectAtIndex:i]objectForKey:@"name"];
        NSString *add=[[array objectAtIndex:i]objectForKey:@"address"];
       
        if (insert_statement == nil) {
            NSString *statement1 = [NSString stringWithFormat:@"insert into tableName (name,address) Values( ?,?)",name,add];
            const char *insertSql = [statement1 UTF8String];
            if(sqlite3_prepare_v2(database, insertSql, -1, &insert_statement, NULL) != SQLITE_OK){
                NSLog(@"Error while creating insert statement.");
                insert_statement = nil;
                continue;
            }
            
        sqlite3_bind_text(insert_statement, 1, [name UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(insert_statement, 2, [add UTF8String], -1, SQLITE_TRANSIENT);
        

        if(SQLITE_DONE != sqlite3_step(insert_statement)){
            //NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
            NSLog(@"Error while inserting data.");
            insert_statement = nil;
            continue;
        }
        else{}
        
            sqlite3_reset(insert_statement);
            sqlite3_finalize(insert_statement);
            insert_statement = nil;
        }
    }
sqlite3_close(database);
}

Why does it allow only one record?

Upvotes: 0

Views: 2911

Answers (3)

Harjot Singh
Harjot Singh

Reputation: 6927

It's occurred due to finalize statement in loop every time. So it wont let insert other objects after the first object.

Just remove "sqlite3_finalize(insert_statement);" from the loop and set nil to the insert statement "insert_statement = nil;"

After done with loop. Make sure place the "sqlite3_reset(insert_statement);" for each object in the loop. It reset the statement and there is no need to set nil to the statement in loop.

Try this piece of code, it works for me:

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {  

    for (int i = 0; i < [array count]; i++)
    {
        NSLog(@"Array Count is %d",[array count]);
        NSString *name=[[array objectAtIndex:i]objectForKey:@"name"];
        NSString *add=[[array objectAtIndex:i]objectForKey:@"address"];
        
        if (insert_statement == nil) {
            NSString *statement1 = [NSString stringWithFormat:@"insert into tableName (name,address) Values( ?,?)",name,add];
            const char *insertSql = [statement1 UTF8String];
            if(sqlite3_prepare_v2(database, insertSql, -1, &insert_statement, NULL) != SQLITE_OK){
                NSLog(@"Error while creating insert statement.");
                insert_statement = nil;
                continue;
            }
            
            sqlite3_bind_text(insert_statement, 1, [name UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(insert_statement, 2, [add UTF8String], -1, SQLITE_TRANSIENT);
            
            
            if(SQLITE_DONE != sqlite3_step(insert_statement)){
                //NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
                NSLog(@"Error while inserting data.");
                insert_statement = nil;
                continue;
            }
            else{
            
            sqlite3_reset(insert_statement);
            
            
            }
        }
    }
    insert_statement = nil;
    sqlite3_finalize(insert_statement);
    sqlite3_close(database);
}

Upvotes: 3

Venk
Venk

Reputation: 5955

You can use :

for (int i = 0; i < [mutArray count]; i++)
    {
       NSString *string = [mutArray objectAtIndex:i];
       // insert query
       insert into table_name ('string') values(column_name);
    }

Ref : How to add nsmutable array into sqlite database table

Upvotes: 0

Deepak Bhati
Deepak Bhati

Reputation: 104

try this code its works for me

      for(int i=0;i<[data count];i++) 
        {
            NSMutableDictionary *myDict = [data objectAtIndex:i];

            selectSql=[NSString stringWithFormat:@"INSERT INTO [commentTable] values('%@','%@')",[myDict valueForKey:@"usertype"],[myDict valueForKey:@"userid"]];

            if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK)
            {
                if (sqlite3_step(statement) == SQLITE_DONE)
                {
                    ret = YES;
                    NSLog(@"DB SUPPORT - commentTable INSERTED");
                }
                else
                {
                    NSLog(@"DB SUPPORT - ERROR commentTable INSERT");
                }

            }
            else
            {
                NSLog(@"DB SUPPORT - Sql Preparing Error ( INSERT commentTable)");
            }

            sqlite3_finalize(statement);
        }


        sqlite3_close(database);

Upvotes: 2

Related Questions