通过rails console执行sql语句


$ RAILS_ENV=production bundle exec rails c

> r = ActiveRecord::Base.connection.execute 'show create database cms_production'
=> #<Mysql2::Result:0x0e61d68 @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=>"1, :port=>3306, :database=>"cms_production", :flags=>2}>
irb(main):009:0> r.each {|row| puts row }
cms_production
CREATE DATABASE `cms_production` /*!40100 DEFAULT CHARACTER SET utf8 */
=> [["cms_production", "CREATE DATABASE `cms_production` /*!40100 DEFAULT CHARACTER SET utf8 */"]]

> r = ActiveRecord::Base.connection.execute 'show tables'
=> #<Mysql2::Result:0x00a8 @query_options={:as=>:array, :async=>false, :cast_booleans=>false, :symbolize_keys=>false, :database_timezone=>:utc, :application_timezone=>nil, :cache_rows=>true, :connect_flags=>21125, :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语句)