Last active
August 29, 2015 13:57
-
-
Save billymeltdown/9402744 to your computer and use it in GitHub Desktop.
Fetching SQLite table column information
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (NSDictionary *)scanTableColumns { | |
// Basically: PRAGMA table_info( your_table_name ); | |
NSMutableDictionary *columns = [[[NSMutableDictionary alloc] init] autorelease]; | |
NSString *sql = [NSString stringWithFormat:@"PRAGMA table_info( '%@' );", [[self class] tableName]]; | |
[[self class] execute:sql withBlock:^(sqlite3_stmt *stmt) { | |
NSMutableDictionary *column = [NSMutableDictionary dictionary]; | |
const unsigned char *cName; | |
NSString *stringName = @"Unknown Column"; | |
cName = sqlite3_column_text(stmt, 1); // name is col 1 | |
if (cName != NULL) { | |
stringName = [NSString stringWithUTF8String:(char *)cName]; | |
[column setObject:stringName forKey:@"name"]; | |
} | |
const unsigned char *cColtype; | |
cColtype = sqlite3_column_text(stmt, 2); // type is col 2 | |
if (cColtype != NULL) { | |
[column setObject:[NSString stringWithUTF8String:(char *)cColtype] forKey:@"type"]; | |
} | |
[columns setObject:column forKey:stringName]; | |
}]; | |
return columns; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment