2012-02-13 189 views
1

這是我調用並傳遞一個變量的SQLite代碼。問題在哪裏?SQLite插入在模擬器中工作,但不在設備上

-(IBAction)insertPet:(NSString *) name{ 

NSString *query = [NSString stringWithFormat:@"Insert into pet (name) values ('%@');", name]; 
sqlite3_stmt *statement; 

NSLog(@"1 - %@",query); 

if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil)==SQLITE_OK) 
{ 

    NSLog(@"2 - %@",query); 

    if(SQLITE_DONE == sqlite3_step(statement)){ 

     NSLog(@"Query - %@",query); 
    } 
    sqlite3_finalize(statement); 
} 
} 
+0

NSLog(@「1 - %@」,查詢)和NSLog(@「2 - %@」,查詢)通過。無法達到NSLog(@「查詢 - %@」,查詢)。 – ksprs8 2012-02-13 13:46:29

+0

如果你告訴我們錯誤信息是什麼,它會有很大的幫助... – 2012-02-13 13:51:50

回答

0

我的猜測是你的數據庫被放置在應用程序包中。如果是這樣的話,您將不得不創建一個可寫入的數據庫副本,以便能夠執行更改數據庫的查詢(如INSERT或UPDATE)。這樣做的最普遍的方式是將數據庫從數據包複製到Documents文件夾。

+0

你能提供一些代碼/建議如何做到這一點? – Richard 2013-05-28 11:16:38

+0

當然,使用[NSFileManager方法](https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/copyItemAtPath:toPath:error :)進行復制。爲了讓你的文件路徑檢查[這裏](http://stackoverflow.com/questions/5508433/using-nssearchpathfordirectoriesindomains-properly) – lawicko 2013-05-28 15:22:06

0

我的工作是從設備上刪除應用程序,重新啓動設備,清除Xcode中的應用程序,然後再次加載它,同時檢查數據庫上是否選擇了Target Membership。

相關問題