tags:

views:

223

answers:

3

Hey all,

i deployed my App to my iPhone and get

Unknown error calling sqlite3_step (8: attempt to write a readonly database) eu on Insert / Update Statements.

On the Simulator it all works like it should.

My sqlite Database is placed in the Resource Folder (Xcode).

Thanks for help!

+1  A: 

Your application bundle is not writable on the iPhone. You MUST copy the file somewhere else, like your documents folder. It works in the simulator because the Mac does not enforce all the sandboxing restrictions the iPhone does.

Louis Gerbarg
Ok, but how does it work to "deliver" a sqlite Database with my app?
phx
+1  A: 

You can copy your database from the application bundle directory to the Documents directory in viewDidLoad. You can read/write from/to your database in the Documents directory after this. Of course, you need to check if the database in the Documents directory exist before you do the copy in order not to overwrite it the next time you bring up the app.

Assuming you have defined your database name '#define kFilename @"yourdatabase.db"' in the .m file.

In viewDidLoad add:


// 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);

}


Good luck!

aobs

aobs
Sorry but didnt really works for me:my path looks like:/var/mobile/Applications/30D76248-1E01-4A60-9986-EA4417762A01/Documents/mytable.sqliteBut i cant write anything to the database :(
phx
A: 

Perhaps you already did, please check the permission of your database.

aobs