1

Here is my code i am getting only the last value of the database in the array but i need the entire column values in my array...

//select the values from the db
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM district_details"];
sqlite3_stmt *statement;

if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
{
    while (sqlite3_step(statement)==SQLITE_ROW)
    {
        char *field2 = (char *)sqlite3_column_text(statement, 1);
        field2Str = [[NSString alloc]initWithUTF8String:field2];
        str = [NSString stringWithFormat:@"%@", field2Str];           NSLog(@"the value from thr field1 from the district name is %@...",field2Str);
        myarray =[[NSMutableArray alloc]initWithObjects:@"--select the value--",nil];
        [myarray addObject:field2Str];
        NSLog(@"the value of the myarray is......%@",myarray);
    }

}
0

2 Answers 2

2

Initiate your array object once before you use it, if you initiate it inside while loop like you do it loses the previous added objects and takes fresh allocation inside the heap memory.

Just put this code inside viewDidLoad method and remove from while loop:

- (void)viewDidLoad {
    [super viewDidLoad];
    // mutable array allocation do once here  
    myarray =[[NSMutableArray alloc]initWithObjects:@"--select the value--",nil];
}

Now use below code to fetch entire column from data base:

//select the values from the db
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM district_details"];
sqlite3_stmt *statement;

if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
{
    while (sqlite3_step(statement)==SQLITE_ROW)
    {
        char *field2 = (char *)sqlite3_column_text(statement, 1);
        field2Str = [[NSString alloc]initWithUTF8String:field2];

        // --------- myarray allocation removed from here -----------

        [myarray addObject:field2Str];
        NSLog(@"the value of the myarray is......%@",myarray);
    }
    sqlite3_finalize(statement);
}
sqlite3_close(db);
Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that you are instantiating a new mutable array in ever loop of your while statement. You want to instantiate it once, before the loop, and inside the loop merely addObject.

NSString *sql = [NSString stringWithFormat:@"SELECT * FROM district_details"];
sqlite3_stmt *statement;

if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)==SQLITE_OK) {
    myarray = [[NSMutableArray alloc] initWithObjects:@"--select the value--",nil];
    while (sqlite3_step(statement)==SQLITE_ROW) {
        char *field2 = (char *)sqlite3_column_text(statement, 1);
        field2Str = [[NSString alloc] initWithUTF8String:field2];
        [myarray addObject:field2Str];
    }
    sqlite3_finalize(statement);  // remember to finalize to prevent SQLite leak    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.