Skip to content

Instantly share code, notes, and snippets.

@billymeltdown
Last active August 29, 2015 13:57
Show Gist options
  • Save billymeltdown/9402744 to your computer and use it in GitHub Desktop.
Save billymeltdown/9402744 to your computer and use it in GitHub Desktop.
Fetching SQLite table column information
+ (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