zdl411437734 / sqlitepersistentobjects

Automatically exported from code.google.com/p/sqlitepersistentobjects
0 stars 0 forks source link

Crash in tableExists in SQLiteInstanceManager #67

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Call [[SQLiteInstanceManager sharedManager] tableExists:@"..."] before 
accessing a persistent 
object.

What is the expected output? What do you see instead?
expect to get true or false, get a null pointer error

Fix:
use [self database] instead of the member variable in the tableExists: method.

Bonus:
here's a new method to see if a column exists in a table, which is great for 
doing schema 
updates:

- (BOOL)columnExists:(NSString *)columnName inTable:(NSString *)tableName {
    BOOL ret = NO;
    NSString *query = [NSString stringWithFormat:@"pragma table_info(%@);", tableName];
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2( [self database],  [query UTF8String], -1, &stmt, nil) == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const char *name = (const char *)sqlite3_column_text(stmt, 1);
            if (name != NULL && [columnName isEqualToString:[NSString 
stringWithCString:name]]) {
                ret = YES;
                break;
            }
        }
        sqlite3_finalize(stmt);
    }
    return ret;
}

Original issue reported on code.google.com by paule...@gmail.com on 28 Apr 2009 at 7:26