SQLite Forum

columnName function
Login
There's a static function in the sqlite3.c file called columnName. I know it's not part of the API but hoped by opening it up I'd be able to get the DB, table and name for the i'th column in a stmt. I can only get it to return a meaningful value for useType 0 & 1 though. Does anyone know why it always seems to return "0.0" for useType 2,3 & 4? That's the case even for simple stmts such as "select col from tbl" where col is a valid column name within table tbl. 

/*
** Convert the N-th element of pStmt->pColName[] into a string using
** xFunc() then return that string.  If N is out of range, return 0.
**
** There are up to 5 names for each column.  useType determines which
** name is returned.  Here are the names:
**
**    0      The column name as it should be displayed for output
**    1      The datatype name for the column
**    2      The name of the database that the column derives from
**    3      The name of the table that the column derives from
**    4      The name of the table column that the result column derives from
**
** If the result is not a simple column reference (if it is an expression
** or a constant) then useTypes 2, 3, and 4 return NULL.
*/
static const void *columnName(
  sqlite3_stmt *pStmt,     /* The statement */
  int N,                   /* Which column to get the name for */
  int useUtf16,            /* True to return the name as UTF16 */
  int useType              /* What type of name */
){
  const void *ret;
  Vdbe *p;
  int n;
  sqlite3 *db;
#ifdef SQLITE_ENABLE_API_ARMOR
  if( pStmt==0 ){
	(void)SQLITE_MISUSE_BKPT;
	return 0;
  }
#endif
  ret = 0;
  p = (Vdbe *)pStmt;
  db = p->db;
  assert( db!=0 );
  n = sqlite3_column_count(pStmt);
  if( N<n && N>=0 ){
	N += useType*n;
	sqlite3_mutex_enter(db->mutex);
	assert( db->mallocFailed==0 );
#ifndef SQLITE_OMIT_UTF16
	if( useUtf16 ){
	  ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]);
	}else
#endif
	{
	  ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]);
	}
	/* A malloc may have failed inside of the _text() call. If this
	** is the case, clear the mallocFailed flag and return NULL.
	*/
	if( db->mallocFailed ){
	  sqlite3OomClear(db);
	  ret = 0;
	}
	sqlite3_mutex_leave(db->mutex);
  }
  return ret;
}