Can anyone please help me out.I have been stuck with this NSMutableArray for last 2-3 days. I am beginner in Objective-C and I don’t know what is the error in the code.In my program, I have created a sqlite database.It contain 3 fields and i have successfully inserted the values into db.And I am extracting these values from the db into NSMutableArray and displaying this array.But the problem is only last element is displaying and not whole data. I dont understand why it is displaying only the last element.I will paste the code below- Can anyone please check it and help me out.
- (void)viewDidLoad
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:@"TDL1.db"];
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &TDLDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT MYTASK, SUBTASK FROM TDL1 "];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(TDLDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
todolist2=[[NSMutableArray alloc]init];
NSString *mytask = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
NSString *subtask = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
todolist2 = [NSMutableArray arrayWithObjects:mytask,subtask, nil];
NSLog(@"THE array is:%@",todolist2);
}
sqlite3_finalize(statement);
}
sqlite3_close(TDLDB);
}
NSLog(@"THE outside array is:%@",todolist2);
[super viewDidLoad];
}
And in the console I am getting the following output :-
2012-09-06 11:33:23.529 TDL[823:f803] THE array is:(
task0,
task0
)
2012-09-06 11:33:23.532 TDL[823:f803] THE array is:(
task1,
task1
)
2012-09-06 11:33:23.533 TDL[823:f803] THE array is:(
task2,
task2
)
2012-09-06 11:33:23.534 TDL[823:f803] THE array is:(
task3,
task3
)
2012-09-06 11:33:23.534 TDL[823:f803] THE array is:(
task4,
task4
)
2012-09-06 11:33:23.535 TDL[823:f803] THE array is:(
task5,
task5
)
2012-09-06 11:33:23.536 TDL[823:f803] THE array is:(
task6,
task6
)
2012-09-06 11:33:23.537 TDL[823:f803] THE array is:(
task6,
task6
)
2012-09-06 11:33:23.537 TDL[823:f803] THE array is:(
task7,
task7
)
2012-09-06 11:33:23.538 TDL[823:f803] THE array is:(
task7,
task7
)
2012-09-06 11:33:23.539 TDL[823:f803] THE array is:(
task8,
task8
)
2012-09-06 11:33:23.539 TDL[823:f803] THE array is:(
task9,
task9
)
2012-09-06 11:33:23.540 TDL[823:f803] THE array is:(
task10,
task10
)
2012-09-06 11:33:23.540 TDL[823:f803] THE outside array is:(
task10,
task10
)
2012-09-06 11:33:23.541 TDL[823:f803] could not prepare statement: not an error
I want to display the values starting from task0 to task10.How to show that?Please tell me the solution.Thanks in advance.
You are just making a new NSMutableArray each time you go through your
while (sqlite3_step(statement) == SQLITE_ROW)loop. Actually you are making one twice. Move the following line outside the while loop:Then inside your while loop instead of recreating a new array each time with:
Instead use:
These lines will add the two strings to the end of your array.