学学习网 手机版

学学习网

学习路径: 学习首页 > 应用开发 > ios >

第九节 用SQLite管理数据库(4)

设置字体:
----------------------------------

- (BOOL)initDatabase{
  BOOL success;
  NSError *error;
  NSFileManager *fm = [NSFileManager defaultManager];
  NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"sample.db"];
 
  success = [fm fileExistsAtPath:writableDBPath];
  if(!success){
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"sample.db"];
    success = [fm copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if(!success){
      NSLog([error localizedDescription]);
    }
    success = NO;
  }
  if(success){
    db = [[FMDatabase databaseWithPath:writableDBPath] retain];
    if ([db open]) {
      [db setShouldCacheStatements:YES];
    }else{
      NSLog(@"Failed to open database.");
      success = NO;
    }
  }
  return success;
}
 
- (void) closeDatabase{
  [db close];
}
 
- (void)dealloc {
  [db release];
  [window release];
  [super dealloc];
}
 
@end
----------------------------------
课程列表
重点难点
赞助链接