0

How to use sqlite database in iphone using xcode 4.3.2.... I am having problem in finding path of my database ?? since i had copied FormDB.sqlite in Database folder in my app....Please help me as i am new to iphone programming...

calling this method in applicationDidFinishLaunching of Appdelegate file but it is returning success=false

- (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:@"FormDB.sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success) 
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }   
}

- (NSString *) getDBPath {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"FormDB.sqlite"];
}
2
  • Process should be the same, bundle SQLite database with app, copy database to Documents or Library directory on first launch, use database. What have you tried thats not working? Commented Apr 19, 2012 at 14:05
  • You probably forgot to copy database to Documents directory at first launch... It's usually dan in (custom) methods with names like createEditableCopyOfDatabaseIfNeeded or similiar. You'll find a nice tutorial here: icodeblog.com/2008/08/19/… Commented Apr 19, 2012 at 14:09

1 Answer 1

2

In AppDelegate.h file declare

@interface AppDelegate{

        NSString *databaseName;
    NSString *databasePath;

}
-(void) checkAndCreateDatabase;
@end

and then in

@implementation AppDelegate{

    - (void)applicationDidFinishLaunching:(UIApplication *)application {
        // Setup some globals
        databaseName = @"FormDB.sqlite";

        // Get the path to the documents directory and append the databaseName
        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [documentPaths objectAtIndex:0];
        databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

        // Execute the "checkAndCreateDatabase" function
        [self checkAndCreateDatabase];

        // Query the database for all animal records and construct the "animals" array
        [self readAnimalsFromDatabase];

        // Configure and show the window
        [window addSubview:[navigationController view]];
        [window makeKeyAndVisible];
    }
    -(void) checkAndCreateDatabase{
        // Check if the SQL database has already been saved to the users phone, if not then copy it over
        BOOL success;

        // Create a FileManager object, we will use this to check the status
        // of the database and to copy it over if required
        NSFileManager *fileManager = [NSFileManager defaultManager];

        // Check if the database has already been created in the users filesystem
        success = [fileManager fileExistsAtPath:databasePath];

        // If the database already exists then return without doing anything
        if(success) return;

        // If not then proceed to copy the database from the application to the users filesystem

        // Get the path to the database in the application package
        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

        // Copy the database from the package to the users filesystem
        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

        [fileManager release];
    }
}
@end

try this code May this Help you.... :-)

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

1 Comment

release the filemanager if success is YES and you return. also [...] around the call to create databasePathFromApp

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.