在 Ruby 中从 MySQL 结果获取列标题名称

发布于 2024-08-17 19:35:26 字数 207 浏览 6 评论 0原文

我正在使用 Ruby mysql 模块。

我想打印出查询结果并包含列名称。我无法找到为我提供这些名称数组的方法。我的值如下所示。

result = my.query("从 foo 中选择 *")

结果.每个做|行|放置 row.join(',') end

感谢您的帮助!

I'm using the Ruby mysql module.

I want to print out the results of a query and include the column names. I'm having trouble finding the method to give me an array of these names. I have the values as shown below.

result = my.query("select * from foo")

result.each do |row| puts row.join(',') end

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪漫人生路 2024-08-24 19:35:26

 result.fetch_fields.each_with_index do |info, i|
       printf "--- Column %d (%s) ---\n", i, info.name
       printf "table:            %s\n", info.table
       printf "def:              %s\n", info.def
       printf "type:             %s\n", info.type
       printf "length:           %s\n", info.length
       printf "max_length:       %s\n", info.max_length
       printf "flags:            %s\n", info.flags
       printf "decimals:         %s\n", info.decimals
     end

 result.fetch_fields.each_with_index do |info, i|
       printf "--- Column %d (%s) ---\n", i, info.name
       printf "table:            %s\n", info.table
       printf "def:              %s\n", info.def
       printf "type:             %s\n", info.type
       printf "length:           %s\n", info.length
       printf "max_length:       %s\n", info.max_length
       printf "flags:            %s\n", info.flags
       printf "decimals:         %s\n", info.decimals
     end
怂人 2024-08-24 19:35:26

这可能不会直接回答您的问题,但您可能会从检查数据库抽象层中受益。其中有几个适用于 Ruby 的,最著名的是 ActiveRecord。 ActiveRecord 作为 Ruby on Rails 框架的模型部分捆绑在一起,但您可以“独立”使用它,无需使用 Rails 的其余部分。

有了它(或类似的东西),您将能够更快速、更准确地创建应用程序,因为它提供了与数据库通信的宝贵工具——您需要花费大量时间自己开发这些工具。如果您选择将数据库供应商从 MySQL 更改为其他数据库(例如 PostgreSQL 或 Oracle),则无需重新编码太多。

This may not directly answer your question, but you might benefit from checking out a database abstraction layer. There are several of them for Ruby, most notably ActiveRecord. ActiveRecord comes bundled as the model part of the Ruby on Rails framework, but you can use it "stand-alone" without the rest of Rails.

With it (or something similar) you will be able to more rapidly and accurately create applications, as it provides invaluable tools to communicating with your database--tools you would spend a lot of time developing yourself. Should you ever choose to change your database vendor from MySQL to something else (PostgreSQL or Oracle, for example), you won't have to recode much.

ぃ双果 2024-08-24 19:35:26

您正在搜索的方法是Mysql2::Result#fields。在示例中:

> result = ActiveRecord::Base.connection.execute('SELECT 1 as "first_column_name", 2 as "second_column_name"')
=> #<Mysql2::Result:0x0000559c8b9d8370...
> result.fields
=> ["first_column_name", "second_column_name"]

The method you are searching for is Mysql2::Result#fields. In example:

> result = ActiveRecord::Base.connection.execute('SELECT 1 as "first_column_name", 2 as "second_column_name"')
=> #<Mysql2::Result:0x0000559c8b9d8370...
> result.fields
=> ["first_column_name", "second_column_name"]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文