Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

sqlite database works fine on simulator, but is readonly on device

yezaevyezaev Posts: 94Registered Users
edited June 2009 in iPhone SDK Development
Anytime I step my insert statements they return result code 8, SQLITE_READONLY. Has anyone seen this?
Post edited by yezaev on

Replies

  • FlyingDiverFlyingDiver Posts: 997Registered Users
    edited April 2009
    yezaev wrote: »
    Anytime I step my insert statements they return result code 8, SQLITE_READONLY. Has anyone seen this?

    Where is the database on the phone? In the resources directory, or did you copy it to the Documents directory? The resources directory is read-only.

    joe
  • vinced45vinced45 Posts: 23Registered Users
    edited April 2009
    Where is the database on the phone? In the resources directory, or did you copy it to the Documents directory? The resources directory is read-only.

    joe

    You might need to create an editable verision. Try this.
    [PHP]
    - (void)createEditableCopyOfDatabaseIfNeeded
    {
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"database_name.sql"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    // NSLog(@"path : %@", writableDBPath);
    if (success) return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database_name.sql"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success)
    {
    NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
    }
    [/PHP]

    Then read from the newly created path
  • yezaevyezaev Posts: 94Registered Users
    edited April 2009
    Ack thanks to both of you! Somehow I've just never ended up needing to write to any files in the resources directory and hadn't realized they were readonly. Many thanks.
  • AthAth Posts: 53Registered Users
    edited June 2009
    Sorry for picking up this thread, but I didn't want to start a new one, since I have a similar problem.

    First, I've already chmoed MyApp.db (database) and my project folder /MyApp/ both to 777, but I still getting the same error as OP. How did you solved/solve this problem specially? Is it possible to set Resources to writable? If yes, how?

    Thanks in advance!
    What the soul hides, blood tells!
  • smashersmasher Posts: 3,858Registered Users @ @ @ @ @
    edited June 2009
    Ath wrote: »
    Sorry for picking up this thread, but I didn't want to start a new one, since I have a similar problem.

    First, I've already chmoed MyApp.db (database) and my project folder /MyApp/ both to 777, but I still getting the same error as OP. How did you solved/solve this problem specially? Is it possible to set Resources to writable? If yes, how?

    Thanks in advance!

    You do not want to write to resources. "Resources" means the bundle, which will get replaced with a new version if you ever release an update/upgrade.

    If your app needs to make changes to the database file, you should use vinced45's code to copy the database to the documents folder, and open it from there.
  • AthAth Posts: 53Registered Users
    edited June 2009
    Smasher, thanks for the detailed explanation! I got it working! :) Although it takes a time to update, but i'll see what's going on... :P

    Thanks again!
    What the soul hides, blood tells!
Sign In or Register to comment.