dimanche 14 juin 2015

Sqlite tutorial overview for my cocoa mac application in objective c

I am having a cocoa application for mac in objective c.

Now, in my app I want to implement sqlite database like I did for ios apps.

As I am new to mac development, I am not sure how to implement this in cocoa application. How to make sqlite connection to my app?

I used sqlite.h and sqlite.m files and used the below code.

-(void)databaseOpen
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [path objectAtIndex:0];
    NSString *myDBnew = [documentsDirectory stringByAppendingPathComponent:@"SchoolApp.sqlite"];
    database = [[Sqlite alloc] init];
    [database open:myDBnew];
}
- (void)createEditableCopyOfDatabaseIfNeeded
{
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];


    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"SchoolApp.sqlite"];


    /*if([[NSFileManager defaultManager] fileExistsAtPath:writableDBPath]){
     [[NSFileManager defaultManager] removeItemAtPath:writableDBPath error:nil];
     }
     */
    success = [fileManager fileExistsAtPath:writableDBPath];

    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"SchoolApp.sqlite"];

    ////NSLog(@"Default : %@",defaultDBPath);
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

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

    }
}

but How to do this in my cocoa application?

Please anyone can guide me.

Thanks...

Aucun commentaire:

Enregistrer un commentaire