2014-06-20 12 views
0

좋아, 그래서 내 iPhone 시뮬레이터 문서에 데이터베이스가 있습니다. 이제 애플리케이션 샌드 박스에 있는지 확인합니다. 내가 가진 코드에서 뭔가 이상한 점이 있습니다.Sqlite DB 해당 테이블이 없습니다

-(NSString *)getsynDbPath 
{ 
    NSString* dataBAse = [[NSBundle mainBundle] pathForResource:@"ddd"ofType:@"sqlite"]; 

    return dataBAse; 
} 

그럼 내가 경로를 테스트 : 그래서 내가 먼저 DB 경로를 얻을

NSString *testData; 
    testData = [self getsynDbPath]; 

    NSFileManager * fileManager = [NSFileManager defaultManager]; 

    BOOL success = [fileManager fileExistsAtPath:testData]; 

    if (success) { 
     NSLog(@"Oh no! There was a big problem!"); 
    } else { 
     //Successfully opened 
     if(sqlite3_open([testData UTF8String], &db)==SQLITE_OK){ 

      NSLog(@"Raise the roof!"); 

      //Calling method to loop through columns 
      [self listOfCols]; 

     } 


    } 
그때 사용자 정의 방법으로 이동

곳 데이터베이스 내부의 열을 통해 내가 루프 :

-(NSArray *)listOfCols{ 

    NSMutableArray *retval = [[[NSMutableArray alloc]init]autorelease]; 

    NSString *query = @"SELECT KEY_ID FROM CON_DETAIL"; 

    sqlite3_stmt *statement; 
     //Does not execute 
    if (sqlite3_prepare_v2(db, [query UTF8String], -1, &statement, nil)==SQLITE_OK) { 

     while (sqlite3_step(statement)==SQLITE_ROW) { 

      int key_id = sqlite3_column_int(statement, 0); 

      NSLog(@"Key ID: %d", key_id); 

      char *nameChars = (char *) sqlite3_column_text(statement, 1); 

      NSLog(@"chars %s", nameChars); 

      char *cityChars = (char *) sqlite3_column_text(statement, 2); 

      NSLog(@"chars %s", cityChars); 
     } 
    } 

    NSLog(@"%s Why '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(db), sqlite3_errcode(db)); 

    return retval; 

} 

여기 내 질문이 있습니다. 데이터베이스를 성공적으로 연 후에 왜 로그 오류가 발생합니까? no such table: CON_DETAIL? 어떤 도움을 주셔서 감사합니다.

+1

데이터베이스에 "CON_DETAIL"이라는 이야기가 있습니까? – Impossible

+0

FMDB와 같은 래퍼를 사용하지 않는 이유는 무엇입니까? 시간 절약 ... :) – bikram990

+0

@Impossible OP는 문서 번들이 아닌 리소스 번들에서 파일을 찾고 있습니다. – rmaddy

답변

1

문서 디렉토리에 db를 복사 한 다음 반입해야한다고 생각합니다. 다음 기능으로 복사하십시오.

-(void) dbconnect{ 

    self.databaseName = @”yourdbname.sqlite”; 

    // Get the path to the documents directory and append the databaseName 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    self.databasePath = [documentsDir stringByAppendingPathComponent:self.databaseName]; 

    // Execute the “checkAndCreateDatabase” function 

    [self checkAndCreateDatabase]; 
} 

-(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:databasePath]; 

    // If the database already exists then return without doing anything 
    if(success) { 
     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 copyItemAtPath:databasePathFromApp toPath:self.databasePath error:nil]; 

    [fileManager release]; 

} 

참고 : 응용 프로그램의 문서 디렉토리에서 db를 가져 오지 못하는 경우 다음을 수행하십시오. 대상 -> "빌드 단계"-> "복사 번들 리소스"그런 다음 특정 파일을 여기에 추가하십시오.

그런 다음 "listOfCols"메소드를 호출하십시오.

+0

self.databaseName name이 (가) 유형 문자열일까요? – user3683815

+0

네, 그것은 nsstring 객체입니다. –

관련 문제