简体   繁体   中英

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!

这是你想要的?

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

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

至少对于sqlite3,这就是所需的输出:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM