On 2008 Jan, 02, at 15:52, Jay Sprenkle wrote:
Did you try to query the table 'sqlite_master'? You can get the schema
for any table by referencing the query results..
Thank you, Jay. I kept getting syntax errors when I tried that;
apparently I don't know how to query the sqlite_master. But while
thinking about it I realized that pragma SQL can be executed by the C
API. So I did this, and it worked.
Jerry
(It's Objective-C code but anyone who's interested should be able to
get the idea.)
- (NSArray*)allColumnNamesInTableNamed:(NSString*)tableName {
// Will return nil if fails, empty array if no columns
void* db = [self db] ; // database, a class instance variable
char* errMsg = NULL ;
int result ;
NSString* statement ;
statement = [[NSString alloc] initWithFormat:@"pragma
table_info(%@)", tableName] ;
char** results ;
int nRows ;
int nColumns ;
result = sqlite3_get_table(
db, /* An open database */
[statement UTF8String], /* SQL to be
executed */
&results, /* Result is in char *[]
that this points to */
&nRows, /* Number of result rows
written here */
&nColumns, /* Number of result columns
written here */
&errMsg /* Error msg written here */
) ;
[statement release] ;
NSMutableArray* columnNames = nil ;
if (!(result == SQLITE_OK)) {
// Invoke the error handler for this class
[self showError:errMsg from:16 code:result] ;
sqlite3_free(errMsg) ;
}
else {
int j ;
for (j=0; j<nColumns; j++) {
if (strcmp(results[j], "name") == 0) {
break ;
}
}
if (j<nColumns) {
int i ;
columnNames = [[NSMutableArray alloc] init] ;
for (i=0; i<nRows; i++) {
[columnNames addObject:[NSString
stringWithCString:results[(i+1)*nColumns + 1]]] ;
}
}
}
sqlite3_free_table(results) ;
NSArray* output = nil ;
if (columnNames != nil) {
output = [columnNames copy] ;
}
[columnNames release] ;
return [output autorelease] ;
}
-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------