vendredi 4 décembre 2015

Why does my SQLite version change when I check it?

I am just getting acquainted with a new code base that relies on the commonly-used FMDatabaseQueue, and I'm trying to apply the following function to it (which I found here: How do I use sqlite3 PRAGMA user_version in Objective-c?)

-(int)queryUserVersion: (sqlite3*) db {
    NSLog(@"inside query user version");
    // get current database version of schema
    static sqlite3_stmt *stmt_version;
    int databaseVersion;

    if(sqlite3_prepare_v2(db, "PRAGMA user_version;", -1, &stmt_version, NULL) == SQLITE_OK) {
        while(sqlite3_step(stmt_version) == SQLITE_ROW) {
            databaseVersion = sqlite3_column_int(stmt_version, 0);
            NSLog(@"%s: version %d", __FUNCTION__, databaseVersion);
        }
        NSLog(@"%s: the databaseVersion is: %d", __FUNCTION__, databaseVersion);
    } else {
        NSLog(@"%s: ERROR Preparing: , %s", __FUNCTION__, sqlite3_errmsg(db) );
    }
    sqlite3_finalize(stmt_version);

    return databaseVersion;
}

I am doing this to help me begin versioning on a system of databases that has never had versioning before. I thought if the pragma user version had not been set it should be zero, and that is what the function above returns the first time. But the function is called several times and after the first call gives a nonsensical very negative number:

result integer is 0
...
result integer is -1074064488
...
result integer is -1074064488

and then a second run:

result integer is 0
...
result integer is -1074158696
...
result integer is -1074158696

So the large negative number is stable once it has changed over, but different between runs. The function is always being called on the same database.

I am calling the function with this wrapper:

- (void) testUserVersionQuery {
      [queue inDatabase:^(FMDatabase *db) {
          NSLog(@"inside test user version query");
          int i = [self queryUserVersion:(__bridge sqlite3 *)(db)];
          NSLog(@"result integer is %d", i);
      }];
}

Why might this number be changing after the first function call?

Aucun commentaire:

Enregistrer un commentaire