我正在尝试将一个破折号小部件连接到在 sqlserver 中运行的 sql 查询。这是我的工作脚本:
require 'mysql2'
current_count = 0
final count = 0
SCHEDULER.every '5m', :first_in => 0 do |job|
final_count = current_count
# Mysql connection
db = Mysql2::Client.new(:host => "xxx.xxx.xx.xx", :username => "xxxxxxx", :password => "xxxxxxxxxx", :port => 3306, :database => "xxxxxxxxxx")
# Mysql query
$sql = "SELECT COUNT(*) AS total FROM dbo.Idea"
# Execute the query
results = db.query(sql)
if(results)
current_count = results['total']
send_event('final_count', {value: current_count})
end
end
我确定我做错了一些简单的事情,但我对 ruby 和 dashing 不熟悉,我不确定从这里去哪里。有没有在 ruby 中调试代码的好方法?具体是这个工作?谢谢
大家好,所以我改变了我的开发环境,然后让 gem 调试器工作,所以我重写了代码如下。当代码运行时,它以以下形式从数据库中提取值{"total"=>93}
,但是该值仍然没有显示在小部件中。我有一个数字小部件和一个仪表小部件,两者都应该显示93
用于测试目的。我确实插入了一个虚拟值以确保链接正常工作。如何格式化结果?我尝试了 JSON 解析,但是它使程序崩溃了。
require 'debugger'
require 'tiny_tds'
SCHEDULER.every '300s', :first_in => 0 do |job|
debugger
client = TinyTds::Client.new(:username => 'xxxxxxx', :password => 'xxxxxxxxxx', :host => 'xxx.xxx.xx.xx')
rs = client.execute("SELECT COUNT(*) AS total FROM xxxxxxxxxx.dbo.Idea")
rs.each do |row|
send_event('final_count', {value: row['value'] })
end
client.close
end