2013-11-25 2 views
3

SQLite Manager firefox addon에서 myDB.sqlite 데이터베이스를 만들었습니다. 이 myDB.sqlite 파일을 프로젝트에 추가하고 싶습니다. 그런 다음 테이블에서 데이터를 가져 오는 함수를 작성할 수 있습니다.별도의 .sqlite 파일을 ios 프로젝트에 추가하십시오.

프로젝트 폴더 &에 myDB.sqlite 파일을 추가하려고 시도했습니다. 이와 같은 파일 경로를 만듭니다. 하지만이 파일 경로가 잘못되었다고 생각합니다.

-(NSString *) filePath{ 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"document.sqlite"]; 
    return path; 
} 

myDB.sqlite 파일을 프로젝트 폴더에 추가하는 것이 맞습니까? 또는이 myDB.sqlite 파일을 어디에 저장해야합니까? & 내가 사용해야하는 파일 경로는 무엇입니까 ????

난 당신이 성공적으로 프로젝트의 폴더에 SQLite는 DB를 복사 한 가정

+0

는에 알아보기 [답을 받아 들일] (http://meta.stackexchange.com/questions/16721/how-does-accept-rate-work) 당신은 당신의 질문에 대한 솔루션을 얻을 때 posted. –

답변

5

프로젝트에 myDB.sqlite 파일을 추가하십시오. 마우스 오른쪽 버튼을 클릭하여 프로젝트 이름으로 => "를"프로젝트 이름은 "다음에 추가 할 파일을 추가합니다. 당신이 방법을 시작으로 한 마무리에 AppDelegate에

- (void) copyDatabaseIfNeeded { 

//Using NSFileManager we can perform many file system operations. 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 

NSString *dbPath = [self getDBPath]; 
BOOL success = [fileManager fileExistsAtPath:dbPath]; 

if(!success) { 

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 

    if (!success) 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
} 
} 

- (NSString *) getDBPath 
{ 
//Search for standard documents using NSSearchPathForDirectoriesInDomains 
//First Param = Searching the documents directory 
//Second Param = Searching the Users directory and not the System 
//Expand any tildes and identify home directories. 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0]; 
//NSLog(@"dbpath : %@",documentsDir); 
return [documentsDir stringByAppendingPathComponent:@"myDB.sqlite"]; 
} 

이를 추가 할 수 있습니다

는 파일 경로를 얻기 위해 전화 [자기 copyDatabaseIfNeeded]

+0

시뮬레이터 폴더에 myDB.sqlite 파일을 저장했을 때. 하지만 그것은 내 테이블이없는 빈 파일입니다. – DilumN

+0

시뮬레이터를 재설정 한 다음 시뮬레이터에서 sqlite 데이터베이스를 복사 해 봅니다. – wasim

+0

그런 다음 "NSInternalInconsistencyException '오류가 발생했습니다 :'메시지로 쓰기가 가능한 데이터베이스 파일을 만들지 못했습니다. '작업을 완료 할 수 없습니다 (코코아 오류 260).' " – DilumN

1

..... 여러분의 귀중한 시간 주셔서 대단히 감사합니다. 이제 앱 번들에 sqlite Database가 있습니다.

그래서 다음 코드를 사용하여 app 번들에서 device/simulator의 문서 디렉토리로 데이터베이스를 복사해야합니다.

- (void) copyDatabaseIfNeeded 
{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0]; 
    // First, test for existence. 
    if (![fileManager fileExistsAtPath:folderPath]) 
     [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error]; //Create folder 

    NSString *dbPath = [folderPath stringByAppendingPathComponent:@"document.sqlite"]; 
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success) 
    { 
     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"document.sqlite"]; 
     success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 
     if (!success) 
      NSAssert1(0, @"document.sqlite : Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    /** Database copy is not exist **/ 
    [self copyDatabaseIfNeeded]; 

    // Override point for customization after application launch. 
    return YES; 
} 
+0

내가 시뮬레이터 폴더에 저장된이 document.sqlite 파일을했을 때. 그러나 그것은 빈 파일이다. (테이블이없는 경우) – DilumN

1
-(void)addSqlFileIfNotExists { 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
NSString *documentDirectory = [paths objectAtIndex:0]; 

NSString *writableDBPath = [documentDirectory stringByAppendingPathComponent:@"document.sqlite"]; 

BOOL exits = [fileManager fileExistsAtPath:writableDBPath]; 
if(exits) return; 

NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"document.sqlite"]; 

BOOL exit = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 

if(!exit) 
    NSLog(@"%@",[error localizedDescription]); 

} 
관련 문제