2013-03-07 7 views
0

Sqlite 데이터베이스를 사용하는 간단한 앱을 작성했습니다. 그것은 아이폰 시뮬레이터에 잘 작동하지만 내 아이폰에서 작동하지 않습니다. 시작 응용 프로그램 후 엑스 코드에실제 장치에서 Sqlite가 작동하지 않습니다 (내 iPhone에서)

-(NSString *) getFilePath 
{ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask,YES); 
NSString *documentsDir=[paths objectAtIndex:0];
 

return [documentsDir stringByAppendingPathComponent:@"database.sql"]; 
} 

-(void)openDatabase 
{ 
    //Open 

    if (sqlite3_open([[self getFilePath] UTF8String], &db) != SQLITE_OK) { 
     sqlite3_close(db); 
     NSAssert(0, @"Database failed to open."); 
    } 
} 

출력 :

2013-03-07 02:12:16.525 SqliteWorkApp[464:907] *** Assertion failure in -[SqliteWorkAppViewController insertRecord], /Users/cmltkt/Objective-C Apps/SqliteWorkApp/SqliteWorkApp/SqliteWorkAppViewController.m:77 
2013-03-07 02:12:16.529 SqliteWorkApp[464:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error updating table.' 
*** First throw call stack: 
(0x3398e2a3 0x3b82797f 0x3398e15d 0x34263ab7 0x19b3b 0x195bd 0x357b5595 0x357f5d79 0x357f1aed 0x358331e9 0x357f683f 0x357ee84b 0x35796c39 0x357966cd 0x3579611b 0x374885a3 0x374881d3 0x33963173 0x33963117 0x33961f99 0x338d4ebd 0x338d4d49 0x357ed485 0x357ea301 0x19147 0x3bc5eb20) 
libc++abi.dylib: terminate called throwing an exception 

insertRecord 기능 :

-(void)insertRecord 
{ 
    NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO 'countries' ('name', 'flag') " "VALUES ('Sample Data','Sample Data')"]; 

    char *err; 
    if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) 
     != SQLITE_OK) { 
     sqlite3_close(db); 
     NSAssert(0, @"Error updating table."); 
    } 
} 
+0

오류는 'insertRecord' 메소드에 있습니다. 해당 메소드에 대한 코드를 표시하십시오. – rmaddy

+0

또한 실제 파일이 장치의 Documents 디렉토리에 있습니까? 서면으로, 귀하의 애플 리케이션은 단순히 비어있는 (존재하지 않는) 데이터베이스를 엽니 다. 빈 테이블이 모두있는 미리 만들어진 데이터베이스 파일을 복사하거나 SQL을 실행하여 테이블을 만들어야합니다. – rmaddy

+0

@rmaddy는 좋은 지적입니다. 번들에 사전 구성된 데이터베이스를 포함시키지 않으면 먼저해야 할 일은 데이터베이스와 스키마를 만드는 것입니다. –

답변

0

내가했다 동일한 문제가, 내가 응용 프로그램에서 SQLite는 DB 휴대용 파일을 사용

과 시뮬레이터에서 잘 작동하지만 실제 장치에서는 작동하지 않습니다.

그래서 파기를 많이 한 후에 sqlite db 파일을 프로젝트로 드래그했을 때 Xcode가 리소스를 번들에 추가하지 않았 음을 발견했습니다.

주세요. 프로젝트를 선택이 방법을

  1. 가서는
  2. 은 (내가 아는대로해야한다. sqlite가) 당신을 database.sqlite 추가 자원을 번들로 파일 "페이즈 빌드"로 이동합니다. enter image description here

    모든 SQLite는 물건을 처리하기위한

, 내 데이터베이스 헬퍼 클래스 코드는

#import "ASCODBHelper.h" 
#import <sqlite3.h> 

@implementation ASCODBHelper 

static ASCODBHelper *db; 


+(ASCODBHelper *)database{ 

    if (db == nil) { 

     db = [[ASCODBHelper alloc] init]; 
    } 

    return db; 
} 

- (id)init{ 

    self = [super init]; 

    if (self) { 

     NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"IOSMeeting" ofType:@"sqlite"]; 

     if (sqlite3_open([sqLiteDb UTF8String], &db) != SQLITE_OK) { 

      NSLog(@"Failed to open database!");  
     } 

    } 
    return self; 
} 

-(void)getPresentationDeatilById:(NSString *)presentationid andSessionId:(NSString *)sessionid{ 

    NSInteger pId = [presentationid integerValue]; 
    NSInteger sId = [sessionid integerValue]; 

    NSString *queryString = [[NSString alloc] initWithFormat:@"SELECT distinct mediaID,mediaURL,meetingName,trackName FROM Media WHERE presentationID='%d' and sessionID ='%d'",pId,sId]; 

    NSLog(@"query is : %@",queryString); 

    sqlite3_stmt *selectStatement; 

    if (sqlite3_prepare_v2(db, [queryString UTF8String], -1, &selectStatement, nil) == SQLITE_OK) { 
     while (sqlite3_step(selectStatement) == SQLITE_ROW) { 

      char *mediaid = (char *) sqlite3_column_text(selectStatement, 0); 
      char *mediaurl = (char *) sqlite3_column_text(selectStatement, 1); 
      char *meetingname = (char *) sqlite3_column_text(selectStatement, 2); 
      char *topicname = (char *) sqlite3_column_text(selectStatement, 3); 

      NSString *mediaID = [[NSString alloc] initWithUTF8String:mediaid]; 
      NSString *mediaURL = [[NSString alloc] initWithUTF8String:mediaurl]; 
      NSString *topicName = [[NSString alloc] initWithUTF8String:topicname]; 
      NSString *meetingName = [[NSString alloc] initWithUTF8String:meetingname]; 

      //you can log here results    

     } 
     sqlite3_finalize(selectStatement); 
    } 
}   

@end 

이며, 여기에 내가 이것을 사용하고 어떻게 코드입니다.

그냥 알려주세요

#import "ASCODBHelper.h" 

을 가져

[[ASCODBHelper database] getPresentationDeatilById:presentationId andSessionId:sessionId]; 

이런 식으로 우리의 DB 도우미 메서드를 호출 할 경우이의 도움이 필요가.

관련 문제