通过rails console执行sql语句
Posted 冰凌花花~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过rails console执行sql语句相关的知识,希望对你有一定的参考价值。
$ RAILS_ENV=production bundle exec rails c
irb(main):008:0> r = ActiveRecord::Base.connection.execute ‘show create database ott_remote_cms_production‘ => #<mysql2::Result:0x002b338fe61d68 @query_options={:as=>:array, :async=>false, :cast_booleans=>false, :symbolize_keys=>false, :database_timezone=>:utc, :application_timezone=>nil, :cache_rows=>true, :connect_flags=>214125, :cast=>true, :default_file=>nil, :default_group=>nil, :adapter=>"mysql2", :encoding=>"utf8", :pool=>5, :username=>"", :password=>"", :host=>"", :port=>3306, :database=>"cms_production", :flags=>2}> irb(main):009:0> r.each {|row| puts row } ott_remote_cms_production CREATE DATABASE `cms_production` /*!40100 DEFAULT CHARACTER SET utf8 */ => [["cms_production", "CREATE DATABASE `ott_remote_cms_production` /*!40100 DEFAULT CHARACTER SET utf8 */"]]
> r = ActiveRecord::Base.connection.execute ‘show tables‘ => #<Mysql2::Result:0x002b338fe01aa8 @query_options={:as=>:array, :async=>false, :cast_booleans=>false, :symbolize_keys=>false, :database_timezone=>:utc, :application_timezone=>nil, :cache_rows=>true, :connect_flags=>214125, :cast=>true, :default_file=>nil, :default_group=>nil, :adapter=>"mysql2", :encoding=>"utf8", :pool=>5, :username=>"", :password=>"", :host=>"", :port=>3306, :database=>"cms_production", :flags=>2}> irb(main):016:0> r.each {|row| puts row } => [] irb(main):017:0> r = ActiveRecord::Base.connection.execute ‘show variables like "%char%"‘; irb(main):018:0* r.each {|row| puts row } character_set_client utf8 character_set_connection utf8 character_set_database utf8 character_set_filesystem binary character_set_results utf8 character_set_server utf8 character_set_system utf8 character_sets_dir /usr/share/mysql/charsets/ => [["character_set_client", "utf8"], ["character_set_connection", "utf8"], ["character_set_database", "utf8"], ["character_set_filesystem", "binary"], ["character_set_results", "utf8"], ["character_set_server", "utf8"], ["character_set_system", "utf8"], ["character_sets_dir", "/usr/share/mysql/charsets/"]]
以上是关于通过rails console执行sql语句的主要内容,如果未能解决你的问题,请参考以下文章