0

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!

3 Answers 3

1

Is this what you are looking for?

$db.execute("SELECT CONCAT(first_name, ' - ', age) as name_and_age FROM info")
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Using sqlite3. Thanks for the comment. This is what helped me most, but I ended up monkey-patching it a little bit. Thank you.
1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.