https://dudfufl.tistory.com/entry/23-SQLite-%EC%BF%BC%EB%A6%AC%EB%AC%B8-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0

  본래 github에 있던 내용인데 사라졌다.


사용법은 이렇고.
  [SqlUtils checkAndCreateDatabase:true];
헤더와 
  + (void) checkAndCreateDatabase:(BOOL)flag;

구현은 이렇다.

#import "FileManager.h"

+ (void) checkAndCreateDatabase:(BOOL)flag {
    NSString *databaseName = @"mbass4.db";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    BOOL success;
    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:databasePath];
    // If the database already exists then return without doing anything
    
    if(success && flag) return;
    
    // If not then proceed to copy the database from the application to the users filesystem
    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    
    // Copy the database from the package to the users filesystem
    [fileManager removeItemAtPath:databasePath error:nil];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    NSLog(@"databasePathFromApp %@ To %@", databasePathFromApp, databasePath);
    
}

https://gist.github.com/tuxcanfly/1205986

 

나 같은 경우 DB 업데이트 할 때마다 db 파일 번호를 올리는데...

DB 구조 변화시 에러를 방지하기 위해서이다. 삼성전자에서 FOTA 할 때는 DB 버전별로 sql 구문을 따로 넣어줬었다.(진짜 개고생...)

물론, 사용자 데이터 보호를 위함이었지만 필드 늘어나고 관계 깨지면 서버에서 걍 다시 다운로드 받는게 맞다.

'Objective-C, SQLite3' 카테고리의 다른 글

nullable, nonnull  (0) 2021.05.26
select 구현 예제  (0) 2021.05.26
sql 공통 .h, .m 작성법  (0) 2021.05.26
iOS Photokit  (0) 2021.05.26
ViewController 자료 교환 방식 수정  (0) 2021.05.20

+ Recent posts