1)
require 'sqlite3'
db = SQLite3::Database.new('development.sqlite3')
db.results_as_hash = true;
rows = db.execute(" SELECT * FROM listings WHERE name = '' OR telephone = '' OR latitude = '' OR longitude = '' ")
rows do |row|
id_num = row['id']
puts "Id = #{id_num}"
dupe_name = row['name']
puts "name = #{dupe_name}"
dupe_tel = row['telephone']
puts "tel = #{dupe_tel}"
dupe_lat = row['latitude']
puts "lat = #{dupe_lat}"
dupe_long = row['longitude']
puts "lat = #{dupe_long}"
puts '===end===='
end
2)
require 'sqlite3'
db = SQLite3::Database.new('development.sqlite3')
db.results_as_hash = true;
db.execute(" SELECT * FROM listings WHERE name = '' OR telephone = '' OR latitude = '' OR longitude = '' ") do |row|
id_num = row['id']
puts "Id = #{id_num}"
dupe_name = row['name']
puts "name = #{dupe_name}"
dupe_tel = row['telephone']
puts "tel = #{dupe_tel}"
dupe_lat = row['latitude']
puts "lat = #{dupe_lat}"
dupe_long = row['longitude']
puts "lat = #{dupe_long}"
puts '===end===='
end
第二个起作用,第一个起作用。
为什么会这样? db.execute
函数不返回结果集。如果是这样,为什么你不能循环呢?
答案 0 :(得分:4)
您是否尝试将rows do |row|
更改为rows.each do |row|
以获取第一个功能?
使用第一个收集集合并将其分配给rows
。您需要在此之后为行创建一个迭代器,以循环通过集合。
第二个函数。execute已经返回一个迭代器。