SQLiteManager4iOS icon indicating copy to clipboard operation
SQLiteManager4iOS copied to clipboard

Create a new database programatically

Open paresh-navadiya opened this issue 9 years ago • 1 comments

Thanks for sharing!

Need to add these requirement.

paresh-navadiya avatar Aug 27 '15 10:08 paresh-navadiya

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;
}

}

paresh-navadiya avatar Aug 27 '15 10:08 paresh-navadiya