如何使用查询基于订单列出结果集

时间:2012-02-07 08:29:26

标签: iphone ios sqlite

//===================================================================================
- ( NSMutableDictionary * ) getDataToDisplayTierTwo:(NSString*)dbPath:(NSString*)iD{
//===================================================================================
    NSMutableDictionary *aTierTwoTemplateData = [[NSMutableDictionary alloc]init];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) 
    {

        NSString *selectSQL = [NSString stringWithFormat: @"select * from sub_categories_reference scr inner join storyboard_sub_categories ssc on ssc.id = scr.sub_category_id inner join subcategory_order as sco on sco.sub_category_id = scr.sub_category_id where  scr.main_category_id = %@ and sco.main_category_id = %@ order by sco.position asc",iD,iD];

        NSLog(@"%@", selectSQL);

        const char *sql_query_stmt = [selectSQL UTF8String];

        sqlite3_stmt *selectstmt;

        if(sqlite3_prepare_v2(database, sql_query_stmt, -1, &selectstmt, NULL) == SQLITE_OK) 
        {

            while(sqlite3_step(selectstmt) == SQLITE_ROW) 
            {
                NSString *aValue = [[NSString alloc] initWithUTF8String:
                                    (const char *) sqlite3_column_text(selectstmt, 6)];

                NSString *aId = [[NSString alloc] initWithUTF8String:
                                 (const char *) sqlite3_column_text(selectstmt, 5)];


               [aTierTwoTemplateData setObject:aId forKey:aValue];

                [aValue release];
                [aId release];

                NSLog(@"%@ %@ ^^^^^^^^^^^^^^^^^^^^picker value id ", aValue, aId);

            }
        }
    }   
    sqlite3_close(database); 


    return aTierTwoTemplateData;
}

当我将它分配给数组时,我能够得到结果集,但是它丢失了我存储在dictionery中的顺序。

实际上,我已经根据位置字段存储了结果集。

当我将结果集分配到数组中时,顺序会发生变化。

请让我知道如何处理这种情况。

这不是重复,因为我在db中有一个coulmn作为“position”

1 个答案:

答案 0 :(得分:0)

如果您想将数据存储为键值对并维护订单,则可以使用NSArrayNSDictionary的组合。
相同的代码将是:


//===================================================================================
- ( NSArray * ) getDataToDisplayTierTwo:(NSString*)dbPath:(NSString*)iD{
//===================================================================================
    NSMutableArray *aTierTwoTemplateData = [[NSMutableArray alloc]init];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) 
    {

        NSString *selectSQL = [NSString stringWithFormat: @"select * from sub_categories_reference scr inner join storyboard_sub_categories ssc on ssc.id = scr.sub_category_id inner join subcategory_order as sco on sco.sub_category_id = scr.sub_category_id where  scr.main_category_id = %@ and sco.main_category_id = %@ order by sco.position asc",iD,iD];

        NSLog(@"%@", selectSQL);

        const char *sql_query_stmt = [selectSQL UTF8String];

        sqlite3_stmt *selectstmt;

        if(sqlite3_prepare_v2(database, sql_query_stmt, -1, &selectstmt, NULL) == SQLITE_OK) 
        {

            while(sqlite3_step(selectstmt) == SQLITE_ROW) 
            {
                NSString *aValue = [[NSString alloc] initWithUTF8String:
                                    (const char *) sqlite3_column_text(selectstmt, 6)];

                NSString *aId = [[NSString alloc] initWithUTF8String:
                                 (const char *) sqlite3_column_text(selectstmt, 5)];



               [aTierTwoTemplateData addObject:[NSDictionary dictionaryWithObject:aId forKey:aValue]];

                [aValue release];
                [aId release];

                NSLog(@"%@ %@ ^^^^^^^^^^^^^^^^^^^^picker value id ", aValue, aId);

            }
        }
    }   
    sqlite3_close(database); 

    return [aTierTwoTemplateData autorelease];
}

通过这种方式,您将拥有一个字典数组,其中您的值将存储在字典中,并且数据的顺序也将被保留。