如何使用PostgreSQL和Ruby将数据库表中的列名提取到数组中? [英] How do I fetch column names from a db table into an array, using PostgreSQL and Ruby?

查看:116
本文介绍了如何使用PostgreSQL和Ruby将数据库表中的列名提取到数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何遍历给定数据库的表中所有列标题,并将每个列名称分别添加到数组中?

How can I iterate through all of the column headers in a table for a given database and add each column name individually to an array?

我知道我可以收集使用 SELECT * FROM my_table 从表中获取所有信息,但是我现在只需要处理列标题。最好的方法是什么?

I know I can gather all of the information from the table using SELECT * FROM my_table but I need to work with just the column headers at this point. What is the best way to accomplish this?

推荐答案

您是否正在使用 pg gem ?如果是这样,您可以尝试以下操作:

Are you using the pg gem? If so, you can try this:

require 'pg'

# `db_config` must contain your database configuration
conn = PG::Connection.open(db_config)

column_names = conn.query("SELECT * FROM my_table LIMIT 1").first.keys

这篇关于如何使用PostgreSQL和Ruby将数据库表中的列名提取到数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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