2013-03-07 143 views
0

我已經編寫了一個使用Sqlite數據庫的簡單應用程序。它在iPhone模擬器上效果很好,但不適用於我的iPhone。在Xcode中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 2013-03-07 00:26:45

+0

另外,設備上的文檔目錄中是否存在實際的文件?正如所寫,您的應用程序只是打開一個空的(不存在的)數據庫。您需要複製預製數據庫文件和所有空表,或者需要執行SQL以創建表。 – rmaddy 2013-03-07 00:28:38

+0

@rmaddy說得很好;除非在軟件包中包含預配置的數據庫,否則首先需要創建數據庫及其模式。 – 2013-03-07 00:33:08

回答

0

我有同樣的問題,

我使用的SQLite Db的便攜式文件中的應用程序,它在模擬器上工作得非常好,但不在真實的設備上。

所以挖了很多之後,我發現,當我將sqlite數據庫文件拖到我的項目中時,Xcode沒有將它添加到綁定資源中。

請!走這條路

  1. 選擇你的項目進入「構建階段」
  2. 添加database.sqlite(它應該是。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" 

,並呼籲我們的數據庫的helper方法這樣

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

讓我知道如果在這個需要幫助。

相關問題