mysql - 在 Rails 中获取 mysql 结果,但格式类似于 MySql 客户端界面

标签 mysql sql ruby-on-rails

我正在使用 Rails 2.2.2 和 Mysql gem v 2.7。如果我从终端启动 MySql 客户端并执行查询,我会得到如下结果表:

mysql> show indexes from lesson_units;
+--------------+------------+-------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table        | Non_unique | Key_name                      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------+------------+-------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| lesson_units |          0 | PRIMARY                       |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| lesson_units |          1 | index_lesson_units_on_user_id |            1 | user_id     | A         |           2 |     NULL | NULL   | YES  | BTREE      |         |               |
+--------------+------------+-------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

Rails中的接口(interface)方法通常做最有用的事情,例如将上述数据作为哈希数组返回,例如

#in Rails console
> ActiveRecord::Base.connection.select_all("show indexes from lesson_units")
=> [{"Index_comment"=>"", "Sub_part"=>nil, "Key_name"=>"PRIMARY", "Comment"=>"", "Collation"=>"A", "Table"=>"lesson_units", "Packed"=>nil, "Seq_in_index"=>"1", "Index_type"=>"BTREE", "Null"=>"", "Cardinality"=>"2", "Non_unique"=>"0", "Column_name"=>"id"}, {"Index_comment"=>"", "Sub_part"=>nil, "Key_name"=>"index_lesson_units_on_user_id", "Comment"=>"", "Collation"=>"A", "Table"=>"lesson_units", "Packed"=>nil, "Seq_in_index"=>"1", "Index_type"=>"BTREE", "Null"=>"YES", "Cardinality"=>"2", "Non_unique"=>"1", "Column_name"=>"user_id"}]

现在,就实际处理数据而言,这非常棒。但是,有时我只想有一个 mysql 风格的 View - 例如,以文本形式返回第一个表,我可以将其打印出来。通过 Rails 这可能吗?即,调用一个返回该字符串的函数?

> ActiveRecord::Base.connection.methodname_here("show indexes from lesson_units")
=> "+--------------+------------+-------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+\n    | Table        | Non_unique | Key_name                      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |\n    +--------------+------------+-------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+\n    | lesson_units |          0 | PRIMARY                       |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |\n    | lesson_units |          1 | index_lesson_units_on_user_id |            1 | user_id     | A         |           2 |     NULL | NULL   | YES  | BTREE      |         |               |\n    +--------------+------------+-------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+\n    2 rows in set (0.00 sec)"

最佳答案

我用过table_print在此之前,在开发中非常方便,因此您可以尝试如下所示:

# Gemfile
group :development, :test do
  gem 'table_print'
end

rails c

result = ActiveRecord::Base.connection.exec_query('show indexes from lesson_units;')

tp result.to_a
TABLE        | NON_UNIQUE | KEY_NAME                      | SEQ_IN_INDEX | COLUMN_NAME | COLLATION | CARDINALITY | SUB_PART | PACKED | NULL | INDEX_TYPE | COMMENT | INDEX_COMMENT
-------------|------------|-------------------------------|--------------|-------------|-----------|-------------|----------|--------|------|------------|---------|--------------
lesson_units | 0          | PRIMARY                       | 1            | id          | A         | 0           |          |        |      | BTREE      |         |              
lesson_units | 1          | index_lesson_units_on_comment | 1            | comment     | A         | 0           |          |        | YES  | BTREE      |         |              

关于mysql - 在 Rails 中获取 mysql 结果,但格式类似于 MySql 客户端界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48804738/

相关文章:

javascript - ( Node js上的Socket.io)用mysql数据更新div停止而不显示错误

php - 在 PHP 中将 2 个不同表中的 1 列匹配在一起

javascript - 来自 MySQL 数组的 Google 图表中的多行

SQL:计算自上次成功以来的天数

mysql - SQL中获取children的记录总数

ruby-on-rails - $http get 请求工作正常,除非我从重定向的外部链接按回

mysql - 一次查询中有两个 "not in"?

MySQL - 根据先前的序列选择序列中的下一项

ruby-on-rails - 从多态关联中链接对象

javascript - 更新 Javascript 中嵌入的 Ruby 值