2013-07-15 151 views
-3

我想更新我的表。此代碼在模擬器上工作,但不能在設備上工作。我發現我不能寫入我的應用程序包中的任何文件,但我沒有如何改變它的工作。請幫幫我。Sqlite更新在模擬器上工作,但不能在設備上工作

-(NSString *)filePath 
{ 
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir=[paths objectAtIndex:0]; 
    NSError *error; 
    bool success; 
    NSString *writablePath = [documentsDir stringByAppendingPathComponent:@"Test.sqlite"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    success = [fileManager fileExistsAtPath:writablePath]; 
    if(!success) 
    { 
     NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Test.sqlite"]; 
     success = [fileManager copyItemAtPath:defaultPath toPath:writablePath error:&error]; 
    } 
    NSLog(@"%@",writablePath); 
    return writablePath; 
} 

然後輸入:

- (IBAction)addToFavorite:(id)sender 
    { 
     [self filePath]; 
     sqlite3_stmt *updateStatement=nil; 
     NSString *sql=[[NSString alloc]initWithFormat:@"UPDATE A SET favorite=? WHERE x='%@' ",mystring]; 
     if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &updateStatement, nil)==SQLITE_OK) 
     { 
      sqlite3_bind_text(updateStatement, 1, [@"1" UTF8String], -1, SQLITE_TRANSIENT); 

     } 
     if (sqlite3_step(updateStatement)!=SQLITE_DONE) 
     { 
      NSAssert(0,@"Error While Creating Update Statement. '%s'",sqlite3_errmsg(db)); 
     } 
     sqlite3_reset(updateStatement);  
    } 
+0

唉唉。我之前有過這個問題。複製你的test.sqlite數據庫。將其重命名爲Test2.sqlite,並將其複製回來。做一個乾淨的。更新你的源代碼成爲test2.sqlite並重新運行它。聽起來很棘手,但這對我來說很有用。 – ApolloSoftware

+0

你不知何故發現或多或少的正確的代碼來設置你的數據庫,從捆綁複製到可寫空間。您需要跟蹤該代碼的執行情況,找出發生了什麼問題。 –

+1

設備區分大小寫。如果你的文件真的命名爲「Test.sqlite」而不是「test.sqlite」(或其他一些變體)? – rmaddy

回答

0

我在模擬器件進行了更改,但在設備上的新數據庫尚未更新。

只是刪除設備和應用的重新運行它即

乾淨安裝

相關問題