2010-09-30 81 views
0

我使用下面的代碼從我的ManagedObjectContext填充數組,但我想要做的是僅提取匹配我的查詢的每行的唯一ID號(itemType = 1),並填充只有這些唯一ID號的fetchResults數組。那可能嗎? 任何幫助表示讚賞。 LQ只提取UniqueID列並填充數組

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

[request setEntity:[NSEntityDescription entityForName:@"MyAppName" 
       inManagedObjectContext:[self managedObjectContext]]]; 

NSError *error = nil;           
NSPredicate *predicate; 
NSArray *fetchResults; 
predicate = [NSPredicate predicateWithFormat:@"(itemType = %i)", 1];    
[request setPredicate:predicate]; 
fetchResults = [managedObjectContext executeFetchRequest:request error:&error]; 

if (!fetchResults) { 
     // NSLog(@"no fetch results error %@", error); 
} 

self.mutableArrayName = [NSMutableArray arrayWithArray:fetchResults]; 
[request release]; 

回答

0

爲了幫助有類似需求的人,我正在回答我自己的問題。我想出了一個解決方案,用於以查詢形式從我的SQLite數據庫中提取特定的行和/或列數據,而不是使用Fetch with Predicate。 (注:代碼顯示用於提取字符串和整數數據類型的例子)

首先,添加一個框架libsqlite3.0.dylib

在標題添加以下文件:

#import <sqlite3.h> 

@interface MyViewController : UIViewController { 
NSMutableArray *dataArray; // This array will hold data you will extract 
NSArray  *summaryArray; // This holds an array of PKIDs that will be queried 
} 

@property (nonatomic, retain) NSMutableArray *dataArray; 
@property (nonatomic, assign) NSArray *summaryArray; 

- (void)getData:(NSInteger *)intPKID; 
- (NSString *) getDBPath; 

@end 

在執行文件添加以下內容:

static sqlite3 *database = nil; // add this before the @implementation line 

@synthesize dataArray; 
@synthesize summaryArray; 

- (NSString *) getDBPath {            
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
     NSString *documentsDir = [paths objectAtIndex:0]; 
     return [documentsDir stringByAppendingPathComponent:@"MyDatabaseName.sqlite"]; 
} 


- (void)getData:(NSInteger *)intPKID {       

     NSString *dbPath = [self getDBPath]; 

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

      NSString *strSQL; 
      NSString *strExtractedData; 
      NSUInteger count; 
      NSInteger intPK; 

      if (self.dataArray == nil) { 
       self.dataArray = [[[NSMutableArray alloc] init] autorelease]; 
      } else { 
       [self.dataArray removeAllObjects];   
      } 

      count = [self.summaryArray count]; 

      for (NSUInteger i = 0; i < count; ++i) { 

       // Extract a specific row matching a PK_ID: 
       strSQL = [NSString stringWithFormat: @"select intPKID, strColumnName from MyDatabaseName where (PKID = %i)", [[self.summaryArray objectAtIndex:i]intValue]]; 

       // Extract a range of rows matching some search criteria: 
       // strSQL = [NSString stringWithFormat: @"select intPKID, strColumnName from MyDatabaseName where (ITEMTYPE = '%i')", 1]; 

       const char *sql = (const char *) [strSQL UTF8String]; 

       sqlite3_stmt *selectstmt; 

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

         while(sqlite3_step(selectstmt) == SQLITE_ROW) { 

          [self.dataArray addObject:[NSNumber numberWithInt:qlite3_column_int(selectstmt, 0)]]; 

          [self.dataArray addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]]; 

         } 
       } 
      } 
     } else { 
      sqlite3_close(database);  // Database not responding - close the database connection 
     } 
} 
0
predicate = [NSPredicate predicateWithFormat:@"itemType == 1"]; 

predicate = [NSPredicate predicateWithFormat:@"(itemType == %i)", 1];

應該都工作。

+0

我的問題是關於從匹配我的查詢謂詞的所有行中提取唯一標識列表。這不會產生唯一ID?它會產生一個非常長的字符串,它看起來像是對行數據的引用。 – 2010-10-01 12:19:51