0

当我tableView使用 sqlite 将两个表数据连接到另一个表时,它会获取 Null 值如何写入NSArrayNSDictionary。以及如何设置对象和键。下面是我的数据库类

+(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];

}
4

1 回答 1

0

您需要在列名之前指定表名。即这个特定列属于哪个表。

请参阅此示例inner join并尝试关联您的代码。

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
于 2013-07-11T13:03:26.783 回答