Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 仅获取UniqueID列并填充数组_Iphone_Xcode_Predicate_Fetch - Fatal编程技术网

Iphone 仅获取UniqueID列并填充数组

Iphone 仅获取UniqueID列并填充数组,iphone,xcode,predicate,fetch,Iphone,Xcode,Predicate,Fetch,我使用下面的代码从ManagedObjectContext填充数组,但我想做的是只获取与我的查询itemType=1匹配的每行的唯一ID号,并仅使用这些唯一ID号填充fetchResults数组。可能吗? 感谢您的帮助。 lq 或 谓词=[NSPredicate predicateWithFormat:@itemType==%i,1] 两者都应该起作用 为了帮助有类似需求的人,我回答了自己的问题。我想出了一个解决方案,可以从我的SQLite数据库中以查询形式提取特定的行和/或列数据,而不是使用带

我使用下面的代码从ManagedObjectContext填充数组,但我想做的是只获取与我的查询itemType=1匹配的每行的唯一ID号,并仅使用这些唯一ID号填充fetchResults数组。可能吗? 感谢您的帮助。 lq

谓词=[NSPredicate predicateWithFormat:@itemType==%i,1]


两者都应该起作用

为了帮助有类似需求的人,我回答了自己的问题。我想出了一个解决方案,可以从我的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
      }
 }

我的问题是如何从匹配查询谓词的所有行中提取唯一ID的列表。这不会产生唯一的ID吗?它生成一个很长的字符串,看起来像是对行数据的引用。
 #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
      }
 }