ooransoy
ooransoy

Reputation: 797

SQLite SELECT not displaying in Ruby console?

When I run this code:

require 'sqlite3'
db = SQLite3::Database.new ":memory:"
  db.execute 'CREATE TABLE foo (bar TEXT);'
  db.execute 'INSERT INTO foo VALUES ("baz");'
  db.execute 'SELECT * FROM foo WHERE bar="baz"'
db.close

I get nothing. Normally in SQLite when I run:

create table foo (bar TEXT);
insert into foo values ("baz");
select * from foo;

I get:

bar       
----------
baz

Why does this happen and how can I get the exact same result?

Upvotes: 0

Views: 182

Answers (1)

falsetru
falsetru

Reputation: 369034

SQLite3::Database#execute method does not print the result. Print the record explicitly.

require 'sqlite3'
db = SQLite3::Database.new ":memory:"
db.execute 'CREATE TABLE foo (bar TEXT);'
db.execute 'INSERT INTO foo VALUES ("baz");'
db.execute 'SELECT * FROM foo WHERE bar="baz"' do |row| # <----
  p row                                                 # <----
end                                                     # <----
db.close

Upvotes: 3

Related Questions