开发者

Sqlite database returns 8 when deployed in ipad

开发者 https://www.devze.com 2023-02-01 12:02 出处:网络
My problem appears in the device but not in the simulator. nDBres=sqlite3_prepare_v2(databaseConn, sqlStatement, -1, &compiledStatement,NULL)

My problem appears in the device but not in the simulator.

nDBres=sqlite3_prepare_v2(databaseConn, sqlStatement, -1, &compiledStatement,NULL)

开发者_开发百科It's an insert query that I'm trying to run. In the simulator it returns 0, whereas in the device it returns 8. After this whenever I try to run any other write operation, the app crashes.

I'm going nuts over this.


You can consult the list of sqlite3 error codes as part of the sqlite3 API documentation (http://www.sqlite.org/c3ref/c_abort.html. Error code 8 is SQLITE_READONLY ("Attempt to write a readonly database").

As you probably know, iOS sandboxes applications running on the device, so you must make sure to create your database in one of the areas that the OS exposes for creating application writable files.

There's a decent tutorial on how to set up a sqlite3 project on iOS http://icodeblog.com/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/.

From that tutorial, the most important part for your issue is probably the createEditableCopyOfDatabaseIfNeeded method in the app delegate. This illustrates how you can ensure that you create an editable database file when your app launches for the first time:

(Note, this isn't my code... I'm reproducing it from the tutorial on icodeblog.com, where they explain it in detail)

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    [self createEditableCopyOfDatabaseIfNeeded];
    [self initializeDatabase];

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}

// Creates a writable copy of the bundled default database in the application Documents directory.
- (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:@"todo.sqlite"];
    success = [fileManager fileExistsAtPath:writableDBPath];

    if (success) return;

    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"todo.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号