2009-11-09 18 views
1

我將我的應用程序部署到我的iPhone上,並在插入/更新語句中獲得如何將iPhone上的ReadOnly更改爲ReadWrite?

Unknown error calling sqlite3_step (8: attempt to write a readonly database) eu

在模擬器上,它的一切都像它應該那樣工作。

我的sqlite數據庫被放置在資源文件夾(Xcode)中。

感謝您的幫助!

+0

也許你已經做了,請檢查你的數據庫的權限。 – aobs 2009-11-16 04:12:51

回答

6

您的應用程序捆綁軟件在iPhone上不可寫。您必須將文件複製到其他地方,如您的文檔文件夾。它可以在模擬器中工作,因爲Mac不會強制執行iPhone所有的沙箱限制。

+0

好吧,但它是如何工作的「交付」與我的應用程序的SQLite數據庫? – phx 2009-11-09 12:11:21

1

您可以將數據庫從應用程序包目錄複製到viewDidLoad中的Documents目錄。此後,您可以在Documents目錄中讀取/寫入/寫入您的數據庫。當然,在複製之前,您需要檢查Documents目錄中的數據庫是否存在,以便在下次啓動應用程序時不覆蓋它。

假設您在.m文件中定義了數據庫名稱'#define kFilename @「yourdatabase.db」'。

在viewDidLoad中添加:


// Get the path to the main bundle resource directory. 

NSString *pathsToReources = [[NSBundle mainBundle] resourcePath]; 

NSString *yourOriginalDatabasePath = [pathsToResources stringByAppendingPathComponent:kFilename]; 

// Create the path to the database in the Documents directory. 

NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [pathsToDocuments objectAtIndex:0]; 

NSString *yourNewDatabasePath = [documentsDirectory stringByAppendingPathComponent:kFilename]; 

if (![[NSFileManager defaultManager] isReadableFileAtPath:yourNewDatabasePath]) { 

if ([[NSFileManager defaultManager] copyItemAtPath:yourOriginalDatabasePath toPath:yourNewDatabasePath error:NULL] != YES) 

NSAssert2(0, @"Fail to copy database from %@ to %@", yourOriginalDatabasePath, yourNewDatabasePath); 

} 

祝你好運!

aobs

+1

抱歉但沒有真正爲我工作: 我的路徑是這樣的: /var/mobile/Applications/30D76248-1E01-4A60-9986-EA4417762A01/Documents/mytable.sqlite ,但我不能寫任何東西到數據庫: ( – phx 2009-11-13 15:01:13

相關問題