zoukankan      html  css  js  c++  java
  • ruby直接底层连接数据库

    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
    ",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
    ",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)
      end
    end
    

      

  • 相关阅读:
    R语言:文本(字符串)处理与正则表达式
    RStudio版本管理 整合Git
    GIT 图形化操作指南
    RStudio 断点调试 进入for循环语句调试
    安装python Matplotlib 库
    win7 32位安装 python 及Numpy、scipy、matplotlib函数包
    ubuntu下hive-0.8.1配置
    我对PageRank的理解及R语言实现
    R 数据类型
    Pig Latin JOIN (inner) 与JOIN (outer)的区别
  • 原文地址:https://www.cnblogs.com/juandx/p/4039097.html
Copyright © 2011-2022 走看看