【发布时间】:2013-07-24 00:14:26
【问题描述】:
我在 Ruby 中使用 MySQL2 来查询数据库。检查查询结果是否为空的直接方法是什么? 代码如下:
require 'mysql2'
client = Mysql2::Client.new(:host => "localhost", :username => "root")
results = client.query("SELECT * FROM users WHERE group='githubbers'")
【问题讨论】:
-
results.empty?有什么问题 -
您确定该方法存在吗?我收到以下错误:
undefined method "empty" for #<Mysql2::Result:0x9a2afcc -
我的错,我认为结果将是一个数组。 mysql2 gem 文档here 表示有一个
count方法 - 你试过这个吗? -
非常感谢:
count工作!