gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:56:09 30 4
gpt4 key购买 nike

我正在使用 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com