Skip to content

Create a new database programatically #12

Description

@paresh-navadiya

Thanks for sharing!

Need to add these requirement.

Activity

  1. paresh-navadiya commented on Aug 27, 2015

    @paresh-navadiya
    Author

    Can someone add these method

    -(NSError *)createNewDatabase:(NSString *)strDataBaseName
    {
    NSString *strDocsDir = nil;
    NSArray *strDirPaths = nil;

    // Get the documents directory
    strDirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //get path from array
    strDocsDir = [strDirPaths objectAtIndex:0];
    
    // Build the path to the database file
    NSString *databasePath = [strDocsDir stringByAppendingPathComponent:strDataBaseName];
    
    NSFileManager *filemgr = [NSFileManager defaultManager];
    
    if ([filemgr fileExistsAtPath: databasePath ] == NO)
    {
        const char *dbpath = [databasePath UTF8String];
    
        if (sqlite3_open(dbpath, &db) == SQLITE_OK)
        {
            //NSLog(@"database created");
            return nil;
        }
        else
        {
            NSString *strError = @"Failed to open/create database";
            NSError *errorQuery = [self createDBErrorWithDescription:strError andCode:kDBFailAtOpen];
            return errorQuery;
        }
    }
    else
    {
        NSString *strError = @"Database already exists with same name";
        NSError *errorQuery = [self createDBErrorWithDescription:strError andCode:kDBFailAtCreate];
        return errorQuery;
    }
    

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions