Sqlite Database Load Fails - Issue with sqlite prepare statement - iPhone - xCode 4.3.1

前端 未结 1 712
春和景丽
春和景丽 2020-12-07 06:16

I am having issues with the following code which loads an SQLite database.

- (NSArray *)getDatabase {

NSLog(@\"Get Database Called\");

    NSMutableArray          


        
相关标签:
1条回答
  • 2020-12-07 06:39

    Yeah i agree With Joachim. there is a problem sometimes the DB doesnot really connect. what i do is a couple of things. First i add following Code in my Application App Delegate.

    - (void) copyDatabaseIfNeeded {
    
        //Using NSFileManager we can perform many file system operations.
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSString *dbPath = [self getDBPath];
        BOOL success = [fileManager fileExistsAtPath:dbPath]; 
    
        if(success) {
    
            NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MyDB.sqlite"];
            success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
    
            if (!success) 
                NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }   
    }
    

    Call This Function in ApplicationDidFinishLaunching.

    Now remove the data base that is in ur bundle currently. (MAKE SURE U HAD BACKUP OF IT). And (if Possible Delete All the project From Iphone Simulator Folder) Coz sometimes the previous Database is attached. Clean Your project, Add the Data Base in ur bundle. Compile it..

    Let Me know if it worked

    the Get Path Function

    - (NSString *) getDBPath {
            //Search for standard documents using NSSearchPathForDirectoriesInDomains
            //First Param = Searching the documents directory
            //Second Param = Searching the Users directory and not the System
            //Expand any tildes and identify home directories.
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
            NSString *documentsDir = [paths objectAtIndex:0];
            return [documentsDir stringByAppendingPathComponent:@"MYDB.sqlite"];
    }
    
    0 讨论(0)
提交回复
热议问题