views:

276

answers:

1

my NSMutableArray creation code is being bypassed altogether for some reason. in theory it is supposed to create an NSMutableArray based on an sqlite database. There is only one warning message and no errors. what am I missing? the implementation file is:

#import "iProspectFresno LiteAppDelegate.h"
#import "MainViewController.h"
#import "Mine.h"
#import <Foundation/Foundation.h>
@implementation iProspectFresno_LiteAppDelegate
@synthesize window;
@synthesize mainViewController;
@synthesize mines;
-(void) checkAndCreateDatabase {
     BOOL success;
 NSFileManager *fileManager = [NSFileManager defaultManager];
 success = [fileManager fileExistsAtPath:databasePath];
 if(success) return;
 NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
 [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 
}
-(void) readMinesFromDatabase 
{
 sqlite3 *database;
 mines = [[NSMutableArray alloc] init];
 NSLog(@"readMinesFromDatabase initialized");
 if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
  const char *sqlStatement = "select * from MinesoftheMotherLode";
  sqlite3_stmt *compiledStatement;
  NSLog(@"first if statement");
  if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
  {
   NSLog(@" second if statement initialized");
   while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
   {
    NSNumber *aentryNumber = [NSNumber numberWithInt:(int)sqlite3_column_int(compiledStatement, 1)];
    NSString  *amineName = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 2)];
    NSString *amineType = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 3)];
    NSString *astatus = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 4)];
    NSNumber *alatitude = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 5)];
    NSNumber *alongitude = [NSNumber numberWithDouble:(double)sqlite3_column_double(compiledStatement, 6)];
    NSString *ametal =[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 7)];
    BOOL *adisplay = NO;
    NSNumber *acoverRegion =[NSNumber numberWithInt:(int)sqlite3_column_int(compiledStatement, 9)];
    NSLog(@"mine", aentryNumber, amineName, amineType, astatus, alatitude, alongitude, ametal, adisplay, acoverRegion);
    Mine *mine = [[Mine alloc] initWithEntryNumber:aentryNumber mineName:amineName mineType:amineType status:astatus latitudeInitial:alatitude longitudeInitial:alongitude metal:ametal display:adisplay coverRegion:acoverRegion];
    [mines addobject:mine];
    [mine release];
   }
 }
  NSLog(@"created database successfully");
  sqlite3_finalize(compiledStatement);
 }
sqlite3_close(database);
    }
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    databaseName = @"MinesoftheMotherLode.sql";
     NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDir = [documentPaths objectAtIndex:0];
 databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
 [self checkAndCreateDatabase];
 [self readMinesFromDatabase];
 MainViewController *aController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
 self.mainViewController = aController;
 [aController release];
    mainViewController.view.frame = [UIScreen mainScreen].applicationFrame;
 [window addSubview:[mainViewController view]];
    [window makeKeyAndVisible];
}

The implementation file for Mines is here:

#import "Mine.h"
@implementation Mine
@synthesize entryNumber, mineName, mineType, status, latitudeInitial, longitudeInitial, metal, display, coverRegion;
-(id)initWithEntryNumber:(NSNumber *)e mineName:(NSString *)n mineType:(NSString *)t status:(NSString *)s latitudeInitial:(NSNumber *)l longitudeInitial:(NSNumber *)o metal:(NSString *)m display:(BOOL *)d coverRegion:(NSNumber *)c
{
 self.entryNumber = e;
 self.mineName = n;
 self.mineType = t;
 self.status = s;
 self.latitudeInitial = l;
 self.longitudeInitial = o;
 self.metal = m;
 self.display = d;
 self.coverRegion = c;
 return self;
}
@end

The NSLog "Second if statement initialized" is not showing up on the console. any ideas as to what needs to be fixed here? and yes I know, I should be using core data.

A: 

It seems you've answered your initial question about it loading. For your secondary question regarding the crash about the null string, you should be loading strings like this:

if (sqlite3_column_text(init_statement, 0) != NULL) {
    self.someString = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement, 0)];
} else {
    self.someString = @"";
}
mjdth
just to clarify, "someString" for me would be like amineType correct? could I use this code in place of each NSString *asomeThing =[NSString..(init_statement, 0)];?
kevin Mendoza
Yes exactly. Replace `self.someString` with `self.amineType` or any other variable and be sure to put the correct column in both the first and second lines of the code I've supplied.Also you do not need to do a check like this for double columns, from what I've seen.
mjdth
ok I get several errors when I implement this for all the string loadings: init_statement undeclared (first use in function), self.amineName brings up the error: request for member amineName not a structure or a union, and lastly all the string variables say that they are undeclared, first use in function during the part where the object "mine" is defined.
kevin Mendoza
ok I found out what was wrong. addobject method was missing a capitalization. thanks! lol
kevin Mendoza