仅获取 UniqueID 列并填充数组

发布于 2024-09-25 22:29:25 字数 856 浏览 8 评论 0原文

我使用下面的代码从 ManagedObjectContext 填充一个数组,但我想做的是仅获取与我的查询匹配的每行的唯一 ID 号 (itemType = 1),并仅使用这些唯一的 ID 填充 fetchResults 数组身份证号码。这可能吗? 任何帮助表示赞赏。 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];

I'm using the code below to populate an array from my ManagedObjectContext, but what I would like to do is to fetch only the unique ID numbers of each row matching my query (itemType = 1) and populate the fetchResults array with only these unique ID numbers. Is that possible?
Any help is appreciated.
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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悍妇囚夫 2024-10-02 22:29:25

为了帮助有类似需求的人,我正在回答我自己的问题。我找到了一个解决方案,可以以查询形式从 SQLite 数据库中提取特定的行和/或列数据,而不是使用带有谓词的 Fetch。 (注意:代码显示了提取字符串和整数数据类型的示例。)

首先,添加 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
      }
 }

To help anyone with similar needs, I'm answering my own question. I figured out a solution that works for pulling specific row and/or column data from my SQLite database in query form, rather than using Fetch with Predicate. (Note: code shows examples for extracting string and integer data types.)

First, add a Framework for libsqlite3.0.dylib

In the header add the following file:

 #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

In the implementation file add the following:

 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
      }
 }
拿命拼未来 2024-10-02 22:29:25
predicate = [NSPredicate predicateWithFormat:@"itemType == 1"];

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

应该都可以。

predicate = [NSPredicate predicateWithFormat:@"itemType == 1"];

or

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

should both work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文