通过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语句的主要内容,如果未能解决你的问题,请参考以下文章

Rails中的sql注入

怎样分析sql语句的执行计划

利用pl/sql执行计划评估SQL语句的性能简析

console.time()及console.timeEnd()用法

流程控制语句

分享知识-快乐自己:初识 Hibernate 概念片