SQLite Store: Create and update database schema version properly
parent
ae10e3e75b
commit
6cf7d1d615
|
@ -594,9 +594,17 @@ static SQLiteStore *g_sharedStore = NULL;
|
|||
|
||||
memset(stmt, 0, sizeof(stmt));
|
||||
|
||||
BOOL dbExists = NO;
|
||||
|
||||
if ([[NSFileManager defaultManager] fileExistsAtPath:g_databasePath])
|
||||
dbExists = YES;
|
||||
|
||||
if (sqlite3_open([g_databasePath UTF8String], &g_database) == SQLITE_OK)
|
||||
{
|
||||
char * error;
|
||||
|
||||
if (!dbExists)
|
||||
{
|
||||
NSArray * schemas = createSchema();
|
||||
|
||||
for (NSString *schema in schemas)
|
||||
|
@ -608,6 +616,15 @@ static SQLiteStore *g_sharedStore = NULL;
|
|||
}
|
||||
}
|
||||
|
||||
NSString * createVersion = [NSString stringWithFormat:@"PRAGMA user_version = %lld", currentSchemaVersion];
|
||||
|
||||
if (sqlite3_exec(g_database, [createVersion UTF8String], NULL, NULL, &error))
|
||||
{
|
||||
DLog(@"SQLite error: %s", error);
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
#define PREPARE(name) (sqlite3_prepare_v2(g_database, query_##name, (int)strlen(query_##name), &stmt[stmt_##name], NULL))
|
||||
|
||||
if (PREPARE(user_version_get))
|
||||
|
|
Loading…
Reference in New Issue