谁能帮帮我。过去 2-3 天我一直被这个 NSMutableArray 困住。我是 Objective-C 的初学者,我不知道代码中的错误是什么。在我的程序中,我创建了一个 sqlite 数据库。它包含 3 个字段,并且我已成功将值插入到数据库中。我正在提取这些值从 db 到 NSMutableArray 并显示这个数组。但问题是只显示最后一个元素而不是整个数据。我不明白为什么它只显示最后一个元素。我将粘贴下面的代码 - 任何人都可以检查它并帮助我。
- (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];
}
在控制台中,我得到以下输出:-
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
我想显示从task0到task10的值。如何显示?请告诉我解决方案。提前致谢。