【问题标题】:Fetch only the UniqueID column and populate array仅获取 UniqueID 列并填充数组
【发布时间】:2010-09-30 17:13:20
【问题描述】:

我正在使用下面的代码从我的 ManagedObjectContext 填充一个数组,但我想做的是只获取与我的查询匹配的每一行的唯一 ID 号 (itemType = 1) 并填充 fetchResults 数组只有这些唯一的 ID 号。那可能吗? 任何帮助表示赞赏。 lq

 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];

【问题讨论】:

    标签: iphone xcode predicate fetch


    【解决方案1】:

    为了帮助有类似需求的任何人,我正在回答我自己的问题。我想出了一个解决方案,它可以以查询形式从我的 SQLite 数据库中提取特定的行和/或列数据,而不是使用 Fetch 和 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
          }
     }
    

    【讨论】:

      【解决方案2】:
      predicate = [NSPredicate predicateWithFormat:@"itemType == 1"];
      

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

      两者都应该有效。

      【讨论】:

      • 我的问题是关于从与我的查询谓词匹配的所有行中提取唯一 ID 列表。这不会产生唯一 ID?它会生成一个很长的字符串,看起来像是对行数据的引用。
      猜你喜欢
      • 2022-11-13
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 2014-03-15
      • 1970-01-01
      • 2012-10-30
      • 2015-03-09
      相关资源
      最近更新 更多