从sqlite数据库中的列返回名称 [英] Return the names from columns in sqlite database

查看:157
本文介绍了从sqlite数据库中的列返回名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在java代码中执行sqlite查询,该代码将从sqlite表返回列名。我怎么能这样做?我只是设法从表中获取值而不是列的名称。

I am trying to perform an sqlite query in java code which will return the columns names from an sqlite table. How can I do so? I am only managed to get the values from the table and not the names of the columns.

推荐答案

使用select ... limit 0查询获取元数据,然后使用ResultSetMetaData对象实际检索它。这样的事情:

Use "select ... limit 0" query to get just metadata, then use ResultSetMetaData objects to actually retrieve it. Something like this:

List<String> sqliteTableColumns(Connection connection, String tableName) {
    List<String> columns = new ArrayList<>();
    String sql = "select * from " + tableName + " LIMIT 0";
    Statement statement = connection.createStatement();
    ResultSet rs = statement.executeQuery(sql);
    ResultSetMetaData mrs = rs.getMetaData();
    for(int i = 1; i <= mrs.getColumnCount(); i++) {
        columns.add(mrs.getColumnLabel(i));
    }
    return columns;
}

这篇关于从sqlite数据库中的列返回名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆