开发者

SQLite works on device, but not in simulator. sqlite3_prepare_v2 fails

开发者 https://www.devze.com 2023-02-08 06:17 出处:网络
I\'m having problems with SQLite on the iPhone simulator. I\'ve reduced it to a simple test app.sqlite3_prepare_v2 fails.But the real device works fine.

I'm having problems with SQLite on the iPhone simulator. I've reduced it to a simple test app. sqlite3_prepare_v2 fails. But the real device works fine.

I created a database using sqlite3, and added it to my resources. In my code, in viewDidLoad, I call createEditableCopyOfDatabaseIfNeeded (which is a common, widely-used method, seen everywhere). And then I call the following:-

- (void) queryDatabase {

    sqlite3 *database;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"myDatabase.sql"];
    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

        NSMutableString *result = [[NSMutableString alloc] init]; 
        const char *sqlStatement = "select * from people";
        sqlite3_stmt *compiledStatement;
      开发者_如何学C  if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            NSString *firstname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
            NSString *surname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
            [result appendFormat:@"%@ %@\n", firstname, surname];
        }
    [resultView setText:result];
    }
    else {
        [resultView setText:@"Problem querying database"];
    }

    sqlite3_finalize(compiledStatement);
    [result release];

    }

    sqlite3_close(database);
}

On the simulator, sqlite3_prepare_v2 doesn't return SQLITE_OK


Can you please tell, what error are you getting? To find error you can use

    if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) != SQLITE_OK) {
    NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));


Ensure you have deleted the app icon from simulator & clean all targets before build & run. This will probably resolve your issues.


Uninstall app from simulator and reinstall may work.

0

精彩评论

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