2013-05-29 62 views
-1

我有SQLite数据库,其中有5列名为:Name,ID,ChildID,ParentID,。如何从SQLite中读取一列并存储在数组中

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

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

这是我从某列GET数组代码:

/*================================================================== 
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

我的朋友有几点要小心。

此代码是正确的,但在行中你错了: [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; 
} 
2

你加入对象array和分配并返回ModiArray

相关问题