2011-08-31 4 views
1

내가 사용 dB로 내 지원 파일 폴더에 DB를 가져 오기 및 연결 시도 :아이폰 SQLite는 문제

NSString *sqliteDb = [[NSBundle mainBundle] pathForResource:@”person” ofType:@”sqlite”]; 
if(sqlite3_open([sqliteDb UTF8String], & database)!=SQLITE_OK) 

내가 삽입있어 코드를 통해 값을 삽입하는 시도하지만 프로그램을 종료하고 값이있는 DB를 검사 할 때 존재하지 않는다. 나는 여기서 무엇이 잘못 될지 모른다.

+0

응용 프로그램에 원본 데이터베이스를 연결 했습니까? 그래 .. 내가 첨부 –

+0

... –

+0

은 IOS에서 sqllite을 잘 모릅니다,하지만 난 당신이 내가 그랬어 데이터베이스 http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application#Implementing_the_Code_to_Save_Data_to_the_SQLite_Database – Seega

답변

1

확인. 이제 해당 데이터베이스를 응용 프로그램의 데이터 저장소가 될 로컬 장소에 복사하십시오. 아래에 주어진 코드는 똑같이 할 것입니다.

NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *docDir = [docPaths objectAtIndex:0]; 
    NSString *dbPath = [[docDir stringByAppendingPathComponent:@"person.rsd"] retain];  
    NSFileManager *fm = [NSFileManager defaultManager]; 
    BOOL success = [fm fileExistsAtPath:dbPath]; 
    if(success) return; 
    NSString *dbPathFromApp=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent :@"person.rsd"]; 
    [fm copyItemAtPath:dbPathFromApp toPath:dbPath error:nil]; 

그런 다음 쿼리를 실행하면 응용 프로그램 시뮬레이터 폴더에 데이터가 삽입됩니다.

+0

고마워 ... 이제 작동! 정확한 문제를 발견했습니다 .. –

0

먼저 person.sqlite db를 응용 프로그램의 문서 디렉토리에 복사 한 다음 작업을 위해 해당 db를 엽니 다.

1

번들의 파일은 읽기 전용입니다. 편집 할 수 없습니다. 작업을 수행하려면 해당 데이터베이스를 응용 프로그램의 캐시, 문서 또는 임시 디렉토리에 복사해야합니다.