ruby - convert mysql result to array or hash -
sql query:-
class test def self.execute_mysql(host, database, query) net::ssh.start('test.com', user, forward_agent: true) |ssh| ssh.exec!("mysql -ppassword -utestuser -h #{host} #{database} -a --execute '#{query}'") end end
command run:-
result = test.execute_mysql('app', 'sample', 'select * foo')
result string:-
id name address age 1 ram 25 2 sam 30 3 jack india 32 . . . . 100 peterson 27
result variable returns string class. suppose returns 100 records.how can loop through each record ?
are looking this?
> result => "id name address age\n1 ram 25\n2 sam 30\n3 jack india 32" > result.split(" ").each_slice(4){|e| print e } => ["id", "name", "address", "age"]["1", "ram", "us", "25"]["2", "sam", "us", "30"]["3", "jack", "india", "32"]
Comments
Post a Comment