2014-01-18 83 views
-1

因此,我試圖將我目前在主包中的數據庫複製到文檔中,以便我可以寫入它,而不是隻能讀取它。現在我有如下,它只是無法找到數據庫,NSLog(「發現數據庫」);沒有出現,所以複製不起作用。我一直在這個工作了一段時間,但我只是無法弄清楚。任何幫助將不勝感激。將SQLite數據庫從包複製到文檔以便寫入?

更新版本

- (IBAction)update:(id)sender { 
    @try { 
    /* 
    NSString *docsPath = [self applicationDocumentsDirectory]; 
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"]; 
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbPath]; 
    */ 

    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString* dbPath = [documentsPath stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"]; 
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbPath]; 

    if (!fileExists) { 
     NSLog(@"Didn't find database"); 
     // get the source path to copy from 
     // NSString *dbSourcePath = [[NSBundle mainBundle] pathForResource:@"StayhealthyExercises-1" ofType:@"sqlite"]; 
     NSString *dbSourcePath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"]; 
     // copy db to documents 
     [[NSFileManager defaultManager] copyItemAtPath:dbSourcePath toPath:dbPath error:nil]; 
    } 
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)) 
    { 
     NSLog(@"An error has occured: %s", sqlite3_errmsg(db)); 
    } 
    const char *sql = "UPDATE stretchingexercises SET isFavorite = 'true' WHERE ID = '1'"; 
    sqlite3_stmt *sqlStatement; 
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK) 
    { 
     NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db)); 
    }else{ 
     if (sqlite3_step(sqlStatement) == SQLITE_DONE){ 
      NSLog(@"Success"); 
     } 
    } 
    sqlite3_finalize(sqlStatement); 
} 
@catch (NSException *exception) { 
    NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db)); 
} 
@finally { 
    sqlite3_close(db); 
} 

} 

舊版本

- (NSString *)applicationDocumentsDirectory { 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
return basePath; 
} 

- (IBAction)update:(id)sender { 
@try { 
    NSString *docsPath = [self applicationDocumentsDirectory]; 
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"]; 
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbPath]; 
    if (!fileExists) { 
     NSLog(@"Found Database"); 
     // get the source path to copy from 
     NSString *dbSourcePath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"]; 
     // copy db to documents 
     [[NSFileManager defaultManager] copyItemAtPath:dbSourcePath toPath:dbPath error:nil]; 
    } 
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)) 
    { 
     NSLog(@"An error has occured: %s", sqlite3_errmsg(db)); 
    } 
    const char *sql = "UPDATE stretchingexercises SET isFavorite = 'true' WHERE ID = '1'"; 
    sqlite3_stmt *sqlStatement; 
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK) 
    { 
     NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db)); 
    }else{ 
     if (sqlite3_step(sqlStatement) == SQLITE_DONE){ 
      NSLog(@"Success"); 
     } 
    } 
    sqlite3_finalize(sqlStatement); 
} 
@catch (NSException *exception) { 
    NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db)); 
} 
@finally { 
    sqlite3_close(db); 
} 

} 
+0

這是因爲你的路徑沒有指向[NSBundle mainBundle],而是指向NSDocumentDirectory或其他。 –

+0

你檢查過文檔文件夾路徑中的文件嗎? – Retro

+0

@retro是它不存在 –

回答

0

試試這個代碼來檢查文件是否存在

NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString* foofile = [documentsPath stringByAppendingPathComponent:@"CopyFileNameFromMainBundle.db"]; 
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile]; 

這應返回NO

+0

好吧,現在我已經用我現在的代碼更新了我的答案,但現在我得到了NSLog(「成功」),但查詢不起作用,並且我知道查詢應該起作用。我試圖刪除應用程序,所以我會擺脫空白數據庫,如果有一個,任何建議? –

1

替換你的代碼中首先得到DBPATH如果塊- (IBAction爲)更新:(ID)發送與下面的代碼:

NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"StayhealthyExercises-1" ofType:@"sqlite"]; 
+0

@retro ok現在我已經用我現在的代碼更新了我的答案,但現在我得到了NSLog(「成功」),但查詢不起作用,並且我知道查詢應該可以工作。我試圖刪除應用程序,所以我會擺脫空白數據庫,如果有一個,任何建議? –

+0

@RobertSaunders - 所以當查詢失敗時,sqlite_errmsg會報告什麼? –

+0

@Rohan查詢dosent失敗它只是劑量對數據庫做任何事情? –

0

是它只是一個有錯誤的日誌問題?對我來說應該是這樣的:

.... 
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbPath]; 
if (!fileExists) { 
    NSLog(@"Didn't Find Database in Documents Directory!"); 
    ... 
} else { 
    NSLog(@"Found Database in Documents Directory."); 
} 
... 
相關問題