93 可读性
 将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

Read SQLite problem
读取SQLite问题

I have NSTableView with two columns, and i wanna fill them entries from SQLite database.

this method is make query for table

-(void)getPersons
{
 NSString *file = [[NSBundle mainBundle] pathForResource:@"persons" ofType:@"db"];
 sqlite3 *database = NULL;
 if (sqlite3_open([file UTF8String], &database)==SQLITE_OK)
 {
 NSLog(@"i open database");
 sqlite3_exec(database,"select name,age from persons", MyCallback, persons, NULL);
 }
 sqlite3_close(database);
}

the problem appers in MyCallback method :

static int MyCallback(void *context, int count, char **values, char **columns)
{
 NSMutableArray *persons = (NSMutableArray *)context;
 for (int i=0; i <count; i++) {
 NSString *columnname = [NSString stringWithUTF8String:columns[i]];
 const char *nameCString = values[i];
 if ([columnname isEqualTo:@"name"]) {
 [persons addObject:[NSString stringWithUTF8String:nameCString]];
 }
 else {
 [ages addObject:[NSString stringWithUTF8String:nameCString]];
 }
 }
 return SQLITE_OK;
}

how can i write"age"entries into NSMutableArray *ages, if i can assign *context just for one NSMutableArray?(in this code, this is NSMutableArray *person)

Surely i should create a separate method for get age entries?

thanks.

时间:原作者:5个回答

...