Hello all,
I’m trying to prepare a sql statement to send to sqlite3 via ruby and
am getting errors when using the query and prepare methods. Here’s my
code:
#!/usr/bin/env ruby
require ‘sqlite3’
db = SQLite3::Database.new( “test.db” )
create the database
sql = <<-eof
CREATE TABLE foobar (foo text, bar int);
INSERT INTO “foobar” VALUES(‘hello’,1);
INSERT INTO “foobar” VALUES(‘world’,2)
eof
db.execute_batch(sql)
querying the database via execute works
db.execute( “select * from foobar” ) do |row|
puts row.join(“\t”) ;
end
querying the database via query method does not work
db.query( “select * from foobar” ) do |row|
puts row.join(“\t”) ;
end
querying the database via a prepare does not work
db.prepare( “select * from foobar” ) do |stmnt|
stmnt.execute do |row|
puts row.join(“\t”) ;
end
end
Below is the output from running the execute, query, and prepare methods
in irb:
irb(main):036:0> # querying the database via execute works
irb(main):037:0* db.execute( “select * from foobar” ) do |row|
irb(main):038:1* puts row.join(“\t”) ;
irb(main):039:1* end
hello 1
world 2
=> nil
irb(main):040:0>
irb(main):041:0* # querying the database via query method does not work
irb(main):042:0* db.query( “select * from foobar” ) do |row|
irb(main):043:1* puts row.join(“\t”) ;
irb(main):044:1* end
NoMethodError: undefined method join' for #<SQLite3::ResultSet:0xb7c6bae4> from (irb):43 from /usr/lib/ruby/1.8/sqlite3/database.rb:278:in
query’
from (irb):42
irb(main):045:0>
irb(main):046:0* # querying the database via a prepare does not work
irb(main):047:0* db.prepare( “select * from foobar” ) do |stmnt|
irb(main):048:1* stmnt.execute do |row|
irb(main):049:2* puts row.join(“\t”) ;
irb(main):050:2* end
irb(main):051:1> end
NoMethodError: undefined method join' for #<SQLite3::ResultSet:0xb7c3c190> from (irb):49 from /usr/lib/ruby/1.8/sqlite3/statement.rb:166:in
execute’
from (irb):48
from /usr/lib/ruby/1.8/sqlite3/database.rb:187:in `prepare’
from (irb):47
$ ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
$ irb -v
irb 0.9.5(05/04/13)
I’ve been looking at the docs[1] and the FAQ[2]. Clearly I’m
overlooking something, but what?
Thanks in advance to any pointers.
[1] http://sqlite-ruby.rubyforge.org/sqlite3/
[2] http://sqlite-ruby.rubyforge.org/sqlite3/faq.html
Regards,
- Robert