0

i want to access data fetched in DBhelper class from database in UIViewcontroller class, i have imported class,created object, methods and all variable are accessible in UIViewcontroller class. when methods are called through uiviewcontroller class they are showing database results or whatever methods are assigned to do but when in use those variables/arrays in uiviewcontroller by objects they return null.even i have implemented prperties and synthesised them too.. here is my code

-(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
  • may i know which part u have edited bro? btw thanks for improving it :) Commented Apr 17, 2013 at 10:47

1 Answer 1

1

your DBhelper class code...

u are initialising array again and again u should use below code

\\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 code @interface Yourtableviewclas: uiviewcontroller{ nsmutablearray *allEnteries ; } @property (nonatimic )nsmutablearray allEnteries @end

Yourtableviewclas.m code

synthesize allEnteries ;
-(void)viewdidload{
yourtableviewclass *obj = [yourtableviewclass alloc]init ];
 nslog("%@"obj.allEnteries );
}
Sign up to request clarification or add additional context in comments.

9 Comments

[thanks alloc]initWitLove]xD..it showed all values of database table in array but another problem is, when i call this array in uitable view class it returns null, even i have implemented property of nsmutuable array , can you help on this please?
objDBhelper=[[DBhelper alloc]init]; NSLog(@"this is table class %@",objDBhelper.allEnteries); [super viewDidLoad]; in viewcontroller i called viewdidload function
if m not wrong i think DBhelper is another class and the code u wrote in question is in different class.so if its correct then use updated answer
and dont initialize allEnteries array in DBhelper class
no no old one was fine, code in question is in dbhelper class, i want to fetch data stored in array declared in dbhelper class to table class... look, 3 classes, A,B,C A=viewcontroller B=Dbhelper, to fetch update insert data in database and to store it in array allEntries C=uitableview class, where i have to use that array to show it in a table view problem is , array shows null when i call it in C (table) class
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.