Re: [sqlite] sqlite3_get_table(); How to get all column names in C?
Jerry Krinock wrote: Is there any way to get a list of all column names in a table via the C API, which works even when there are no data rows in the table? Jerry, You can get the column names for any query, including a "select * from table", using the prepared statement APIs. After you prepare a query using sqlite3_prepare(), you can call sqlite3_column_count() to get the number of columns that will be returned, and then loop calling sqlite3_column_name() to get the columns names. Note, you do *not* need to execute the query to get this information, so it doesn't matter if the table is empty or not. After you have the column names you can simply finalize the prepared statement. See http://www.sqlite.org/c3ref/prepare.html, http://www.sqlite.org/c3ref/column_count.html, and http://www.sqlite.org/c3ref/column_name.html for more details. HTH Dennis cote - To unsubscribe, send email to [EMAIL PROTECTED] -
Re: [sqlite] sqlite3_get_table(); How to get all column names in C?
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[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] -
Re: [sqlite] sqlite3_get_table(); How to get all column names in C?
Did you try to query the table 'sqlite_master'? You can get the schema for any table by referencing the query results.. On Jan 2, 2008 4:52 PM, Jerry Krinock <[EMAIL PROTECTED]> wrote: > Is there any way to get a list of all column names in a table via the > C API, which works even when there are no data rows in the table? > -- The PixAddixImage Collector suite: http://groups-beta.google.com/group/pixaddix SqliteImporter and SqliteReplicator: Command line utilities for Sqlite http://www.reddawn.net/~jsprenkl/Sqlite Cthulhu Bucks! http://www.cthulhubucks.com - To unsubscribe, send email to [EMAIL PROTECTED] -
[sqlite] sqlite3_get_table(); How to get all column names in C?
Is there any way to get a list of all column names in a table via the C API, which works even when there are no data rows in the table? The example given for sqlite3_get_table() indicates that the column names are returned as the "zeroth" row. Indeed, it states: "In general, the number of values inserted into azResult will be ((*nrow) + 1)*(*ncolumn)." However, I when the table has no data rows, the returned ncolumn=0, so the expression evaluates to 0 and indeed I get 0 values. Thanks, Jerry Krinock (Using sqlite 3.4.0, as shipped in Mac OS X version 10.5) - To unsubscribe, send email to [EMAIL PROTECTED] -