2011-11-10 185 views
0

我正在開發一個iPad應用程序。
我的資源文件夾中有一個SQLite數據庫。
當我的應用程序啓動時,這些代碼將數據庫複製到我的NSDocumentDirectory。無法從SQLite數據庫檢索值

- (void) copyDatabaseIfNeeded { 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSString *dbPath = [self getDBPath]; 
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(!success) { 

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Mydata.sqlite"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error]; 

    if (!success) 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 

- (NSString *) getDBPath { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 
    return [documentsDir stringByAppendingPathComponent:@"Mydata.sqlite"]; 
} 

在我的應用程序的另一種觀點,我有這些代碼來打開數據庫和檢索一些值。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [paths objectAtIndex:0]; 
if(sqlite3_open([[documentsDir stringByAppendingPathComponent:@"Mydata.sqlite"] UTF8String], &db) != SQLITE_OK){ 
    sqlite3_close(db); 
    NSAssert(0, @"Failed to open database."); 
} 
NSString *sql = [[NSString alloc] initWithFormat:@"SELECT HOMEONE, HOMETWO, HOMETHREE, HOMEFOUR FROM STUDENTS WHERE NAME='%@'",Name]; 
sqlite3_stmt *statement; 
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)== SQLITE_OK) 
{ 
    while(sqlite3_step(statement) == SQLITE_ROW){ 
     char *one = (char *) sqlite3_column_text(statement,0); 
     ActionOne = [[NSString alloc] initWithUTF8String:one]; 
     char *two = (char *) sqlite3_column_text(statement,1); 
     ActionTwo = [[NSString alloc] initWithUTF8String:two]; 
     char *three = (char *) sqlite3_column_text(statement,2); 
     ActionThree = [[NSString alloc] initWithUTF8String:three]; 
     char *four = (char *) sqlite3_column_text(statement,3); 
     ActionFour = [[NSString alloc] initWithUTF8String:four]; 
    } 
    sqlite3_finalize(statement); 
} 

是的,使用這些代碼,我可以從數據庫中檢索值。
但現在我需要檢索更多的值,所以我編輯了我的代碼。

NSString *sql = [[NSString alloc] initWithFormat:@"SELECT HOMEONE, HOMETWO, HOMETHREE, HOMEFOUR, VOICEONE, VOICETWO, VOICETHREE, VOICEFOUR FROM STUDENTS WHERE NAME='%@'",Name]; 
sqlite3_stmt *statement; 
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)== SQLITE_OK) 
{ 
    while(sqlite3_step(statement) == SQLITE_ROW){ 
     char *one = (char *) sqlite3_column_text(statement,0); 
     ActionOne = [[NSString alloc] initWithUTF8String:one]; 
     char *two = (char *) sqlite3_column_text(statement,1); 
     ActionTwo = [[NSString alloc] initWithUTF8String:two]; 
     char *three = (char *) sqlite3_column_text(statement,2); 
     ActionThree = [[NSString alloc] initWithUTF8String:three]; 
     char *four = (char *) sqlite3_column_text(statement,3); 
     ActionFour = [[NSString alloc] initWithUTF8String:four]; 

     char *vone = (char *) sqlite3_column_text(statement,4); 
     VoiceOne = [[NSString alloc] initWithUTF8String:vone]; 
     char *vtwo = (char *) sqlite3_column_text(statement,5); 
     VoiceTwo = [[NSString alloc] initWithUTF8String:vtwo]; 
     char *vthree = (char *) sqlite3_column_text(statement,6); 
     VoiceThree = [[NSString alloc] initWithUTF8String:vthree]; 
     char *vfour = (char *) sqlite3_column_text(statement,7); 
     VoiceFour = [[NSString alloc] initWithUTF8String:vfour]; 
    } 
    sqlite3_finalize(statement); 
} 

現在它甚至不檢索HOMEONE,HOMETWO,HOMETHREE,HOMEFOUR值。 我試着改變我的SQL語句來檢索VOICEONE,VOICETWO,VOICETHREE,VOICEFOUR值,但它也沒有給我任何值。

我做了一些檢查,發現該應用程序甚至沒有踏進這行:

if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)== SQLITE_OK) 

這意味着sqlite3_prepare_v2不成功? 我檢查了數據庫字段類型。
我試圖在'SQLite數據庫瀏覽器'中執行SQL語句,並且它能夠返回值。

我可以知道我的代碼有什麼問題嗎?

+0

我認爲代碼中沒有主要問題請使用數據庫驗證下面字段的拼寫。 VOICEONE,VOICETWO,VOICETHREE,VOICEFOUR – objectivecdeveloper

回答

1

可能會發生此問題,因爲您可能會更改您的數據庫並複製新的並忘記從模擬器或設備中刪除應用程序,並嘗試運行該項目。我建議,如果你在模擬器中運行它,然後plz複製數據庫路徑,並從SQLite數據庫瀏覽器中打開它,並嘗試在這裏運行你的sql語句,你會發現它不在這裏運行。如果您將新數據庫複製到項目中,請從模擬器或設備中刪除應用程序。

+0

謝謝!這解決了我的問題! – Lloydworth