2012-03-14 6 views
0

저는 iOS 5.1을 대상으로하고 응용 프로그램 파일에서 내 문서 폴더로 데이터베이스를 복사하려고합니다. 나는 과거에이 동일한 코드로 이것을 해왔으므로 이번에는 왜 작동하지 않는지 조금 혼란 스럽다.데이터베이스를 복사 할 수 없습니다

이것은 존재하는지 확인하고 그렇지 않은 경우 복사하는 데 사용하는 방법입니다. 그것은 here입니다.

내가 이렇게 이렇게하려면 DB를 조회하려고

-(void) checkAndCreateDatabase{ 
// Check if the SQL database has already been saved to the users phone, if not then copy it over 
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:self.databasePath]; 

// If the database already exists then return without doing anything 
if(success) return; 

NSLog(@"Installing db: %@", self.databasePath); 
// 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 copyItemAtPath:databasePathFromApp toPath:self.databasePath error:nil]; 

//[databasePathFromApp release]; 
//[fileManager release]; 

} :

sqlite3 *database1; 

// Open the database from the users filessytem 
if(sqlite3_open([self.databasePath UTF8String], &database1) == SQLITE_OK) { 
    // Setup the SQL Statement and compile it for faster access 

    NSString *sqlStatement = [NSString stringWithFormat: @"update login set is_logged=0"]; 
    sqlite3_stmt *compiledStatement; 

    if(sqlite3_prepare_v2(database1, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) { 

     if(SQLITE_DONE != sqlite3_step(compiledStatement)){ 
      NSAssert1(0, @"Error while updating logged. '%s'", sqlite3_errmsg(database1)); 
      NSLog(@"Error setting logged_in to 0: %s", sqlite3_errmsg(database1)); 
     } 
     else{ 
      NSLog(@"Made Logged_in=0"); 
     } 
    } 
    else{ 
     NSLog(@"Prop problem1: %s", sqlite3_errmsg(database1)); 
     NSLog(@"Couldn't prep 1"); 
    } 
    // Release the compiled statement from memory 
    sqlite3_finalize(compiledStatement); 
} 
else{ 

    NSLog(@"Couldn't even open db1"); 
} 
sqlite3_close(database1); 

sqlite3_open() 기능 no such table: loginsqlite3_errmsg로이 경우에는 false를 돌려줍니다.

이 데이터베이스 개체를 사용하는 개체를 만드는 매 초마다 호출되는 함수가 있습니다. 데이터베이스가 그 초에 복사되지 않았고 다음 호출이 이전 사본을 인터럽트 할 수 있습니까? 그럴 가능성이 없다.

어떤 아이디어가 문제 일 수 있습니까?

솔루션 는이 질문 상담 : here 및 소수점 숫자 2에 따라 내 데이터베이스가 "복사 번들 리소스"목록에없는 것을 보인다. 나는 그것을 추가했고 모든 것이 이제는 괜찮아 보인다.

나를 올바른 방향으로 생각하게 해줘서 고마워.

+1

Organizer를 사용하여 전화에서 파일을 다운로드하고 복사 된 내용을 확인하십시오. (Apple의 새로운 5.1 표준을 사용하려면 DB 파일을 "백업하지 마십시오"라고 표시해야합니다.) –

+0

문서 폴더에는 내용이 비어 있지만 내용이 비어 있습니다. 나는 이것이 SQL 파일의 인코딩 문제와 관련 있다고 가정 할 것입니다. – emachine

+0

스크래치, 방금 애플 리케이션을 삭제하고 다시 설치하고 데이터베이스가 문서 폴더에 없습니다. [empty] 데이터베이스 파일은 쿼리를 시도한 후에 만 ​​나타나는 것으로 보입니다. – emachine

답변

1

복사 절차의 각 단계에서 유효한 개체가 반환되는지 확인하십시오. 예를 들어 리소스 경로를 검색 할 때 확장을 포함하지 않을 수 있습니다. NSBundle pathForResource:ofType 함수를 사용하여 번들에서 물건을 얻는 경향이 있습니다. 또한 빈 DB에 관해서 sqlite의 open 함수는 데이터베이스가 존재하지 않으면 데이터베이스를 생성하고 데이터베이스를 엽니 다. copyItemAtPath

+0

Error Domain = NSCocoaErrorDomain Code = 260 "작업을 완료 할 수 없습니다 (코코아 오류 260)."UserInfo = 0x271dd0 {NSFilePath =/var /mobile/Applications/F39FFDFB-32FB-4533-8EE2-5ECA8A7FBF60/MyApp.app/myappdb.sql, NSUnderlyingError = 0x271d30 "작업을 완료 할 수 없습니다. 해당 파일이나 디렉토리가 없습니다."} – emachine

+0

[[NSBundle mainBundle ] PathForResource : @ "myappdb"ofType : @ "sql"]이 오류가 발생합니다. - 'NSFileManager copyItemAtPath : toPath : error :] : 원본 경로가 nil 인 경우 캐치되지 않은 예외'NSInvalidArgumentException ' ' – emachine

+0

그래서 내 문제는 내 리소스 폴더에 원래 데이터베이스를 가져 오는 데 사용하는 경로입니다 ... – emachine

관련 문제