用于从sqlite DB获取数据的NSObject类通过NSmutuable返回null到UItableview类

时间:2013-04-17 10:33:13

标签: iphone objective-c xcode sqlite

我想从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;
}

1 个答案:

答案 0 :(得分: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

Yourtableviewclas.m代码

synthesize allEnteries ;
-(void)viewdidload{
yourtableviewclass *obj = [yourtableviewclass alloc]init ];
 nslog("%@"obj.allEnteries );
}