1

Hi, I am trying to get array from StudentDbwithsearchbarViewController class to SearchBarDB class but resultant array not having any data it is giving null Array. Please help me out with this.
thanks in advance

#import <UIKit/UIKit.h>
@interface StudentDbwithsearchbarViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> { 
IBOutlet UITextField *txtMarks,*txtSname;
IBOutlet UITableView *tableStudents;
NSMutableArray *arrStudents;

}
@property(nonatomic,retain)  NSMutableArray *arrStudents;

-(IBAction)saveStudentDetails;
-(IBAction)gotoSearchpage;
@end

@implementation StudentDbwithsearchbarViewController
@synthesize arrStudents;
- (void)viewDidLoad {
    [super viewDidLoad];

    arrStudents = [[DbStudent getStudentRecords]retain];
    NSLog(@"%@",arrStudents);
    NSLog(@"%d",[arrStudents retainCount]);
}

#import "DbStudent.h"
+(NSMutableArray*)getStudentRecords{

    NSArray *arrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *strDestPath = [NSString stringWithFormat:@"%@/Student5.sqlite",[arrDocPath objectAtIndex:0]];
    NSMutableArray *arrStudents = [[NSMutableArray alloc]init];
    sqlite3 *db;
    if(sqlite3_open([strDestPath UTF8String], &db)==SQLITE_OK)
    {

        NSString *query = @"SELECT * FROM  Student";
        void* v;
        char* err_msg;
        sqlite3_stmt *studentStmt;
        if(sqlite3_prepare_v2(db, [query UTF8String], -1, &studentStmt, &err_msg)==SQLITE_OK)
        {
            while (sqlite3_step(studentStmt)==SQLITE_ROW) {

                int sno = sqlite3_column_int(studentStmt, 0);
                NSString *sname = [NSString stringWithUTF8String: sqlite3_column_text(studentStmt, 1)];
                float marks = sqlite3_column_double(studentStmt, 2);

                Student *st = [[Student alloc]init];
                st.Sno = sno;
                st.Sname = sname;
                st.marks = marks;
                [arrStudents addObject:st];
            }

        }

    }
    return arrStudents;


}

#import "SearchBarDB.h"
#import"StudentDbwithsearchbarViewController.h"

- (void)viewDidLoad {

    [super viewDidLoad];
    StudentDbwithsearchbarViewController *sbd = [[StudentDbwithsearchbarViewController alloc]init];
    NSLog(@"%d",[sbd.arrStudents retainCount]);
    NSLog(@"%@",sbd.arrStudents);

//  arrstudentBase = [sbd.arrStudents copy];
    arrMatchedString = [[NSMutableArray alloc]init];
}
2
  • The usual cause for this problem with novices is not understanding objects. When you set a value in one object, it does not magically appear in another separately-created object of the same class. Commented Jan 19, 2012 at 22:12
  • (But in your case it appears to be a little different -- you're referencing arrStudents before you set it. Put NSLog statements where you set and reference the value and observe the order that they come out, then consider what may be happening.) Commented Jan 19, 2012 at 22:15

1 Answer 1

1

Lots of potential problems, no definitive answer without a clue as to what you've tried or how you've determined that it failed.

  • methods should not be prefixed with get; just call it studentRecords or fetchStudentRecords

  • your memory management code is all over the place; you'll be leaking that array, at the least.

  • retainCount is useless, don't call it.

  • writing raw SQL is a waste of time; at least use a wrapper like FMDB or, better yet, move to CoreData

Best guess for failure: the database doesn't exist or the query fails. Have you stepped through the query code?

Sign up to request clarification or add additional context in comments.

5 Comments

retainCount is occasionally useful for finding storage bugs, but, of course, only if you pretty much know what the heck you're doing in the first place. And lots of fairly smart people still write raw SQL.
Smart people often like re-inventing the wheel (I've certainly been known to do that), but that doesn't make the wheel re-invention any less of a re-invention... There are extremely rare cases where raw SQL is required, but they are few and far between.
I think one thing is that smart people don't like to devote brain cells to remembering ten different ways to do SQL.
so by using sqlite it is quit tough to get the result .i not even closed the data base ,When i checked in first class (StudentDbwithsearchbarViewController) the array containing objects but when i import in sub class and created object and trying to access the same super class array it is showing Null
so i should go for coredata frame then i will be able to do that.... but i don't know that much about Coredata frame work.do you have and good tutorial for Coredata

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.