2.3.0 :066 > r=client.query("update test set name = 'minitest' where id=12")
=> nil
2.3.0 :067 > r.class
=> NilClass
2.3.0 :068 > r=client.query("select * from test where id=12")
=> #<Mysql2::Result:0x00000001b3b248 @query_options={:as=>:hash, :async=>false, :cast_booleans=>false, :symbolize_keys=>false, :database_timezone=>:local, :application_timezone=>nil, :cache_rows=>true, :connect_flags=>2147525125, :cast=>true, :default_file=>nil, :default_group=>nil, :host=>"192.168.100.105", :username=>"xxx", :password=>"xxx"}>
2.3.0 :069 > r.each do |x|
2.3.0 :070 > puts x
2.3.0 :071?> end
{"id"=>12, "name"=>"minitest"}
=> [{"id"=>12, "name"=>"minitest"}]
2.3.0 :072 >
本地检查
mysql> select * from test where id=12;
+------+----------+
| id | name |
+------+----------+
| 12 | minitest |
+------+----------+
1 row in set (0.01 sec)
mysql>
2.3.0 :073 > r=client.query("delete from test where id=12")
=> nil
2.3.0 :074 > r=client.query("select * from test where id=12")
=> #<Mysql2::Result:0x00000001960590 @query_options={:as=>:hash, :async=>false, :cast_booleans=>false, :symbolize_keys=>false, :database_timezone=>:local, :application_timezone=>nil, :cache_rows=>true, :connect_flags=>2147525125, :cast=>true, :default_file=>nil, :default_group=>nil, :host=>"192.168.100.105", :username=>"xxx", :password=>"xxx"}>
2.3.0 :075 > r.each do |x|
2.3.0 :076 > puts x
2.3.0 :077?> end
=> []
2.3.0 :078 >
2.3.0 :082 > r=client.query("select * from test limit 10")
=> #<Mysql2::Result:0x00000001c72800 @query_options={:as=>:hash, :async=>false, :cast_booleans=>false, :symbolize_keys=>false, :database_timezone=>:local, :application_timezone=>nil, :cache_rows=>true, :connect_flags=>2147525125, :cast=>true, :default_file=>nil, :default_group=>nil, :host=>"192.168.100.105", :username=>"xxx", :password=>"xxx"}>
2.3.0 :083 > r.each do |x|
2.3.0 :084 > puts x
2.3.0 :085?> end
{"id"=>1, "name"=>"hello1"}
{"id"=>2, "name"=>"hello2"}
{"id"=>3, "name"=>"hello3"}
{"id"=>4, "name"=>"hello4"}
{"id"=>5, "name"=>"hello5"}
{"id"=>6, "name"=>"hello6"}
{"id"=>7, "name"=>"hello7"}
{"id"=>8, "name"=>"hello8"}
{"id"=>9, "name"=>"hello9"}
{"id"=>10, "name"=>"hello10"}
=> [{"id"=>1, "name"=>"hello1"}, {"id"=>2, "name"=>"hello2"}, {"id"=>3, "name"=>"hello3"}, {"id"=>4, "name"=>"hello4"}, {"id"=>5, "name"=>"hello5"}, {"id"=>6, "name"=>"hello6"}, {"id"=>7, "name"=>"hello7"}, {"id"=>8, "name"=>"hello8"}, {"id"=>9, "name"=>"hello9"}, {"id"=>10, "name"=>"hello10"}]
2.3.0 :086 >
可以对这个结果集做些手脚,以更方便操作
2.3.0 :111 > r.class
=> Mysql2::Result
2.3.0 :112 > r.to_a.class
=> Array
2.3.0 :113 > r.to_a[1]
=> {"id"=>2, "name"=>"hello2"}
2.3.0 :114 > r.to_a[1]["id"]
=> 2
2.3.0 :115 > r.to_a[1]["name"]
=> "hello2"
2.3.0 :116 > r.to_a[0]["name"]
=> "hello1"
2.3.0 :117 > r.to_a[9]["id"]
=> 10
2.3.0 :118 >
这个 gem 已经在 Linux 和 Mac OS X 上以下版本的 Ruby 中通过测试
这个 gem 已经通过以下版本的 MySQL 和 MariaDB 的测试
ruby -v
gem source -l
gem install mysql2
irb
原文地址
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。