当我tableView
使用 sqlite 将两个表数据连接到另一个表时,它会获取 Null 值如何写入NSArray
和NSDictionary
。以及如何设置对象和键。下面是我的数据库类
+(NSMutableArray *)getData
{
sqlite3 *dbobj;
NSString *dbpath =[DBClass connectdb];
NSMutableArray *readArray=[[NSMutableArray alloc]init];
if(sqlite3_open([dbpath UTF8String], &dbobj)==SQLITE_OK)
{
sqlite3_stmt *statement=nil;
//**** NSString *string=@"SELECT name FROM emptable";
NSString *string=@"SELECT EMP_ID, Age, DEPT FROM COMPANY INNER JOIN DEPARTMENT ON COMPANY.ID = DEPARTMENT.EMP_ID";
;
const char *query=[string UTF8String];
if(sqlite3_prepare_v2(dbobj, query, -1, &statement, NULL)==SQLITE_OK)
{
while (sqlite3_step(statement)==SQLITE_ROW)
{
NSMutableDictionary *readDic=[[NSMutableDictionary alloc] init];
[readDic setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)] forKey:@"DEPT"];
//[readDic setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)] forKey:@"Age"];
[readArray addObject:readDic];
// NSLog(@"%@",readDic);
}
}
sqlite3_finalize(statement);
}
NSLog(@"%@",readArray);
sqlite3_close(dbobj);
return readArray;
}
下面是我的JointviewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
array=[[NSMutableArray alloc]init];
data=[[NSMutableArray alloc]init];
array=[DBClass getData];
for( NSDictionary *obj in array)
{
NSLog(@"%@",obj);
[data addObject:[obj objectForKey:@"DEPT"]];
//[data addObject:[obj objectForKey:@"Age"]];
}
NSLog(@"\n%d",[data count]);
[Table reloadData];
}