here how i done this create a class called DBManagement
and in m file implement these methods.also put this line in implementation file above @implementation line
static sqlite3 *CHManagement = nil;
-(NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *MyDatabasePath = [documentsDirectory stringByAppendingString:@"/CHManagement.sqlite"];
return MyDatabasePath;
}
-(void) checkDataBase
{
NSLog(@"CheckDatabase Called");
NSFileManager *fileManager=[NSFileManager defaultManager];
NSError *error=[[[NSError alloc]init] autorelease];
NSString *dbPath = [self getDBPath];
BOOL success=[fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSString *defaultDBPath=nil;
defaultDBPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"CHManagement.sqlite"];
success=[fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if(!success)
{
NSAssert1(0,@"failed to create database with message '%@'.",[error localizedDescription]);
}
}
else
{
sqlite3 *MyDatabase;
NSInteger VersionNumber=0;
sqlite3_stmt *CompiledStatement=nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *MyDatabasePath = [documentsDirectory stringByAppendingString:@"/CHManagement.sqlite"];
if(sqlite3_open([MyDatabasePath UTF8String],&MyDatabase) == SQLITE_OK)
{
sqlite3_prepare_v2(MyDatabase, "select appVersionNo from VersionInfo", -1, &CompiledStatement, NULL);
while(sqlite3_step(CompiledStatement) == SQLITE_ROW)
{
VersionNumber=sqlite3_column_int(CompiledStatement,0);
}
sqlite3_reset(CompiledStatement);
sqlite3_finalize(CompiledStatement);
sqlite3_close(MyDatabase);
}
if (VersionNumber!=1)
{
[self deleteDatabase];
NSString *defaultDBPath=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"CHManagement.sqlite"];
success=[fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if(!success)
{
NSAssert1(0,@"failed to create database with message '%@'.",[error localizedDescription]);
}
}
}
}
- (void)deleteDatabase
{
NSLog(@"Delete Database Called");
NSError **error=nil;
NSFileManager *FileManager = [NSFileManager defaultManager];
NSString *databasepath = [self getDBPath];
BOOL success = [FileManager fileExistsAtPath:databasepath];
if(success) {
[FileManager removeItemAtPath:databasepath error:error];
}
}
with these methods you can easily load database into iphone document folder of the app.
Sorry about the formatting. and appVersionNo is a field in a tableCalled versionInfo the default value is 1