0

我想访问从 UIViewcontroller 类的数据库中获取的 DBhelper 类中的数据,我已经导入了类、创建的对象、方法和所有变量都可以在 UIViewcontroller 类中访问。当通过 uiviewcontroller 类调用方法时,它们显示数据库结果或分配要执行的任何方法,但是当通过对象在 uiviewcontroller 中使用这些变量/数组时,它们返回 null。即使我也实现了 prperties 并合成了它们.. 这是我的代码

-(NSMutableArray *) SaveDBInArray {

    [self createEditableCopyOfDatabaseIfNeeded];
    [self initializeDatabase];

    const char *sql = "SELECT * FROM table11";
    if (sqlite3_prepare_v2(database, sql, -1, &init_statement, NULL) != SQLITE_OK) {
        //NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));   }
    while (sqlite3_step(init_statement) == SQLITE_ROW) {
          colum1= [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement,0)];
        colum2= [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement,1)];
        allEnteries=[[NSMutableArray alloc]init];
         [allEnteries addObject:colum1];
        [allEnteries addObject:colum2]; 
       }
    NSLog(@"array with values %@",allEnteries );
//HERE WHENEVER METHOD IS BEING CALLED IT IS DISPLAYING ONLY TOP ONE VALUE FROM EACH COLUM BUT I WANT ALL OF THEM

    sqlite3_reset(init_statement);
    [self closeDatabase];
    return allEnteries;
}
4

1 回答 1

1

您的 DBhelper 类代码...

你一次又一次地初始化数组你应该使用下面的代码

\\Initialize array here
 yourtableviewclass*yourclass= [yourtableviewclassalloc]init];
 dbH.allEnteries=[[NSMutableArray alloc]init];
 \\\\\\\ you must create property of allEnteries array in yourtableviewclassclass and 
 synhtesize it. and then try to access that array with following 2 line code in ur    tableview class
 yourtableviewclass *obj = [yourtableviewclass alloc]init ];
 nslog("%@"obj.yourtableviewclass );


 while (sqlite3_step(init_statement) == SQLITE_ROW) {
      colum1= [NSString stringWithUTF8String:(char     *)sqlite3_column_text(init_statement,0)];
    colum2= [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement,1)];

     [yourclass.allEnteries addObject:colum1];
    [yourclass.allEnteries addObject:colum2]; 
   }

Yourtableviewclas.h 代码 @interface Yourtableviewclas: uiviewcontroller{ nsmutablearray *allEnteries ; } @property (nonatimic)nsmutablearray allEnteries @end

Yourtableviewclass.m 代码

synthesize allEnteries ;
-(void)viewdidload{
yourtableviewclass *obj = [yourtableviewclass alloc]init ];
 nslog("%@"obj.allEnteries );
}
于 2013-04-17T10:49:18.937 回答