stevenspiel
stevenspiel

Reputation: 5999

Formatting SQL output in Ruby

How can I add characters and chain two db queries in the puts statement of Ruby? I'm using sqlite 3

The desired output I want is

Sam - 32

I imagine the code would look something like this:

puts $db.execute(SELECT first_name FROM info) + " - " + $db.execute(SELECT age FROM info)

I know that there is an issue with converting the string to an array. Any help would be appreciated!

Upvotes: 0

Views: 771

Answers (3)

stevenspiel
stevenspiel

Reputation: 5999

At least with sqlite3, this is what gives the desired output:

puts $db.execute(SELECT first_name || ' - ' || age FROM info)

Upvotes: 1

Denis de Bernardy
Denis de Bernardy

Reputation: 78443

It's unclear which SQL library you're using, but I suspect this should get you in the right direction:

$db.execute( "select * from table" ) do |row|
  p row
end

http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html

Upvotes: 1

usha
usha

Reputation: 29349

Is this what you are looking for?

$db.execute("SELECT CONCAT(first_name, ' - ', age) as name_and_age FROM info")

Upvotes: 1

Related Questions