Return the names from columns in sqlite database [

2020-05-03 11:14发布

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.

标签: java sqlite
1条回答
太酷不给撩
2楼-- · 2020-05-03 11:57

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;
}
查看更多
登录 后发表回答