【问题标题】:to get to the column name or column count in result set of query?获取查询结果集中的列名或列数?
【发布时间】:2015-01-30 07:57:21
【问题描述】:
  1. SQL:

    select a.a, a.b, b.c from a, b where a.a=b.a;
    
  2. SQL:

    select '*' from table;
    
  3. SQL:

    select '*' from table1, table2 where table1.a=table2.a;
    
  4. SQL:

    select '*' from table1 left join table2 on table1.a=table2.a;
    

案例1是知道sql结果集中的列名(OK)。 情况 2 可以找到列名(用于pragma table_info (table);)(确定)。 但是情况3、4是如何获取结果集中column count的列名呢?

【问题讨论】:

  • 如果您只想查看结果集,只需调用 DatabaseUtils.dumpCursor()

标签: android sqlite select join


【解决方案1】:

你可以像这样遍历返回的Cursor

 for (int i = 0; i < cursor.getColumnCount(); i++) {
      Log.d("tag", cursor.getColumnName(i) + " -> " + cursor.getType(i));
 }

注意 cursor.getType() 将是以下之一:

static final int FIELD_TYPE_NULL = 0;
static final int FIELD_TYPE_INTEGER = 1;
static final int FIELD_TYPE_FLOAT = 2;
static final int FIELD_TYPE_STRING = 3;
static final int FIELD_TYPE_BLOB = 4;

【讨论】:

    【解决方案2】:

    查询返回Cursor,您可以使用例如getColumnName().

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多