如何编写NSArrays和NSDictionaries以获取和将两个表的数据连接到另一个表SQLite,iPhone?

时间:2013-07-11 11:19:59

标签: ios objective-c sqlite tableview

当我使用sqlite将两个表数据连接到另一个tableView时,它获取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];

}

1 个答案:

答案 0 :(得分:0)

您需要在列名前指定表名。 I.S.该特定列属于哪个表。

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

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;