gpt4 book ai didi

mysql - ruby SQL : undefined method `[]'

转载 作者:行者123 更新时间:2023-11-29 09:17:23 24 4
gpt4 key购买 nike

我觉得我很蠢。

rc = Mysql.new('CENSORED_HOST','username','password','db')
release = rc.query('select * from wp_ribcage_releases where release_id = 1 limit 1')

puts release["release_title"]

rc.close

我收到以下错误:

ribcage-connect.rb:17: undefined method `[]' for #<Mysql::Result:0x1011ee900> (NoMethodError)

我对此很陌生,我确信我基本上在做一些非常愚蠢的事情。非常感谢。

编辑

下面的好心人让我更进一步,现在我有了:

rc = Mysql.new('server','user','pword','db')

release = rc.query('select * from wp_ribcage_releases where release_id = 1 limit 1')

row = release.fetch_row
puts row['release_title']

rc.close

现在我收到错误:

ribcage-connect.rb:12:in `[]': can't convert String into Integer (TypeError)
from ribcage-connect.rb:12

最佳答案

rc.query(...) 返回一个可以迭代的结果集。您无法直接访问它。

看:

http://www.kitebird.com/articles/ruby-mysql.html#TOC_7

由于您只有一行(由于 LIMIT 1),您可以执行以下操作:

row = release.fetch_hash
puts row['release_title']

如果有多行:

while row = release.fetch_hash do
puts row["release_title"]
end

关于mysql - ruby SQL : undefined method `[]' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3661595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com