-1

我有 5 列名为的 SQLite 数据库:Name, ID, ChildID, ParentID, BrotherID

在这个数据库中,我有很多记录,我想将所有列值之一存储在数组中并返回这个数组。例如,我想获取ParentID列中的所有值。我使用这个查询代码:

Select ParentID from Table1(表 1 是表的名称)

这是我从特定列获取数组的代码:

/*==================================================================
 METHOD FOR GETTING MIDIFIED FROM DATABASE
 ==================================================================*/
- (NSMutableArray*)readingModified
{
     ModiArray = [[NSMutableArray alloc] init];
    // Setup the database object
    sqlite3 *database2;
    // Open the database from the users filessytem
    if(sqlite3_open([[self DatabaseSqlite] UTF8String], &database2) == SQLITE_OK)
    {
        // Setup the SQL Statement and compile it for faster access
        //SQLIte Statement
        NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select ParentID from Table1"];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database2, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
        {
            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                // Init the Data Dictionary
                NSMutableDictionary *_dataDictionary2=[[NSMutableDictionary alloc] init];
                NSString *_recordParentID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                [_dataDictionary2 setObject:[NSString stringWithFormat:@"%@",_recordModified] forKey:@"ParentID"];
                [array addObject:_dataDictionary2];
            }
        }
        else
        {
            NSLog(@"No Data Found");
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database2);

    return ModiArray;
}

请告诉我我的错误。

4

2 回答 2

4

我的朋友几个小心点。

这段代码是正确的,但你错了: [array addObject:_dataDictionary2];

而是arrayModiArray

/*==================================================================
 METHOD FOR GETTING MIDIFIED FROM DATABASE
 ==================================================================*/
- (NSMutableArray*)readingModified
{
     ModiArray = [[NSMutableArray alloc] init];
    // Setup the database object
    sqlite3 *database2;
    // Open the database from the users filessytem
    if(sqlite3_open([[self DatabaseSqlite] UTF8String], &database2) == SQLITE_OK)
    {
        // Setup the SQL Statement and compile it for faster access
        //SQLIte Statement
        NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select ParentID from Table1"];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database2, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
        {
            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                // Init the Data Dictionary
                NSMutableDictionary *_dataDictionary2=[[NSMutableDictionary alloc] init];
                NSString *_recordParentID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                [_dataDictionary2 setObject:[NSString stringWithFormat:@"%@",_recordModified] forKey:@"ParentID"];
                [ModiArray addObject:_dataDictionary2];
            }
        }
        else
        {
            NSLog(@"No Data Found");
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database2);

    return ModiArray;
}
于 2013-05-29T11:42:09.523 回答
2

您正在添加对象array并分配和返回ModiArray.

于 2013-05-29T11:44:39.117 回答