Class Mysqltest #Code here require "MySQL" Def testmysql dbc=mysql.real_connect (' localhost ', ' root ', ' 123 ', ' Ivic_porta L ') res=dbc.query (' select * from Virtual_machines ') puts "Test Mysql ...." While Row=res.fetch_row do puts "# {row[0]} "end End Def createtable dbc=mysql.real_connect (' localhost ', ' root ', ' 123 ', ' ivic_portal ') res = Dbc.quer Y ("Select Id,hostname,internet_tx,internet_rx,traffic_tx,traffic_rx from Virtual_machines") puts "===select Data===/ n "While row = Res.fetch_row do printf ("%s,%s,%s,%s,%s,%s\n ", row[0],row[1],row[2],row[3],row[4],row[5]) en D puts "==================/n" puts "Server version:" + dbc.get_server_info Rescue Mysql::error = e puts "Error code: #{e.errno}" puts "error message: #{e.error}" puts "error SQLSTATE: #{e.sqlstate}" if e.respond_to? ("SQLState") ensure puts "Close Connection ..." Dbc.close if DBC end def Insertinto Dbc=mysql.real_co Nnect (' localhost ', ' root ', ' 1(' ivic_portal ') res = dbc.query ("Select Id,hostname,internet_tx,internet_rx,traffic_tx,traffic_rx from Virtual_ Machines ") while row = Res.fetch_row do id = row[0] a = rand (+) B = rand (+) c = rand (1000) D = rand (puts) a,b,c,d printf ("%s,%s,%s,%s,%s,%s\n", row[0],row[1],row[2],row[3],row[4],row[5]) Dbc.query ("Update virtual_machines set internet_tx=internet_tx+#{a} where id = #{id}") Dbc.query ("Update virtual_ma Chines set INTERNET_RX=INTERNET_RX+#{B} where id = #{id} ") Dbc.query (" Update virtual_machines set traffic_tx=traffic _TX+#{C} WHERE id = #{id} ") Dbc.query (" Update virtual_machines set traffic_rx=traffic_rx+#{d} where id = #{id} ") End Rescue Mysql::error = e puts "error code: #{e.errno}" puts "error message: #{e.error}" puts "Er Ror SQLSTATE: #{e.sqlstate} "if e.respond_to? ("SQLState") ensure puts "Close Connection ..." dbc.close if DBC end while true do (Mysqltest.new). Insertinto (mysqltest.new). CreateTable Sleep (5) EndEnd
Ruby Direct underlying connection database