<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d4684235500622716427\x26blogName\x3dCaiwangqin\x27s+blog\x26publishMode\x3dPUBLISH_MODE_HOSTED\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttp://blog.caiwangqin.com/search\x26blogLocale\x3dzh_CN\x26v\x3d2\x26homepageUrl\x3dhttp://blog.caiwangqin.com/\x26vt\x3d3393395200455623441', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>

Caiwangqin's blog

Focus on Cloud Service, Smart Hardware, Architecture, Technic and beyond…

Ruby code snippets for create/delete mysql database

2006年12月1日星期五

liaozhigang写了一个建立、删除数据库Mysql的小程序, 在测试一些Ruby on Rails开源项目时能够帮助快速建立数据库,用法:


Ruby [-d] create_db.rb db_name, -d表示删除数据库。



说明:比如 Ruby create_db.rb hello, 将建立hello_development, hello_test和hello_production三个数据库.


代码:


#create_db.rb
require ‘optparse’
class TableCreate
  def initialize
    @cmd = ‘”C:\Program Files\MySQL\MySQL Server 5.0\bin\mysql.exe” -u root -p’
    @dbs = [’_development’, ‘_test’, ‘_production’]
    @tmp_file_name = ‘_temp.sql’
    @is_delete = false
  end
  def make_sql_file(dbname)
    tmp = File.new(@tmp_file_name, ‘w’);
    operation = @is_delete ? ‘drop ‘ : ‘create ‘
    @dbs.each do |db_ext|
      tmp.puts(”#{operation} database #{dbname}#{db_ext};”)
    end
    tmp.close
  end
  def operate_db(dbname)
    make_sql_file(dbname)
    system(”#@cmd <#@tmp_file_name”)
  end
end
opts = OptionParser.new
opts.on(”-d”)  {|val| @is_delete=true}
rest = opts.parse(ARGV)
if rest.length < 1
  puts “Usage: Ruby [-d] create_db.rb db_name”
else
  TableCreate.new.operate_db(rest[0])
end



标签:

posted by Jesse Cai, 上午9:26

0 Comments:

发表评论

订阅 帖子评论 [Atom]

<< 主页