On 04-Apr-2008, at 1:17 PM, Nicolas Williams wrote:
> On Fri, Apr 04, 2008 at 01:06:58PM -0700, Steven Fisher wrote:
>>> It's not necessarily the same as strcasecmp().  You can have per-
>>> column collations.
>>
>> Column names, not column contents. :) I don't like to have my C code
>> rely on the order of columns from a query. You can avoid depending on
>> parameter ordering with sqlite3_bind_parameter_index, but there
>> doesn't seem to be an equivalent for result columns.
>
> Sure there is:
>
>    const char *sqlite3_column_decltype(sqlite3_stmt*,int);
>    int sqlite3_column_type(sqlite3_stmt*, int iCol);

This would be useful but, again, that's not at all what I want. I'm  
looking for column NAMES, not contents.

Maybe it'd be better to explain this with psuedo code.

This is what I want to do:

   sqlite3_prepare_v2( db, "SELECT ColumnA,ColumnB FROM ATable;", -1,  
&stmt, &tail );
   int column_a_idx = sqlite3_column_index( stmt, "ColumnA" );
   int column_b_idx = sqlite3_column_index( stmt, "ColumnB" );
   while ( sqlite3_step( db ) == SQLITE_ROW ) {
      sqlite3_column_text( stmt, column_a_idx, avalue );
      sqlite3_column_text( stmt, column_b_idx, bvalue );
   }
   sqlite3_fianlize( stmt );

I'm avoiding hard an expectation here that column a is in position 0,  
and column b in position 1. This doesn't matter for such a simple  
query, but for larger queries future proofing the code from changes to  
queries is just good practice.

This code won't run, though, because sqlite3_column_index doesn't  
exist. I need to write my own. That means I need to replace  
sqlite3_column_index with find_column, which is defined something like  
this:

int find_column( sqlite3_stmt * stmt, const char * name )
{
   int count = sqlite3_column_count( stmt );
   for ( int i = 0; i < count; i++ ) {
     const char * column = sqlite3_column_name( stmt, i );
     if ( stricmp( column, name ) == 0 )
       return i;
   }
   return -1;
}

There's two problems here:
1. I need to define something to find the column at all. There's a way  
to find binding indexes by name, so why not columns? I understand the  
need to avoid code bloat, but surely a way to future proof code by not  
having to hard-coding column positions is worth the size delta.
2. I need to use stricmp for comparing column names. I'd rather use  
the same comparison that sqlite3 uses for comparing column NAMES.

The first problem could be fixed by adding sqlite3_column_index, the  
second by adding sqlite3_stricmp. The first would (probably?) increase  
the size of sqlite3 slightly, the second would only make an internal  
function publicly available.
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to