ruby直接底層串連資料庫

來源:互聯網
上載者:User

標籤:blog   io   os   sp   資料   div   on   cti   log   

class MysqlTest  #Code here  require "mysql"  def testMysql    dbc=Mysql.real_connect(‘localhost‘,‘root‘,‘123‘,‘Ivic_Portal‘)    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.query("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])    end    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_connect(‘localhost‘,‘root‘,‘123‘,‘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(1000)      b = rand(1000)      c = rand(1000)      d = rand(1000)      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_machines 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 "Error 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直接底層串連資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.