十二月
01

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

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg

Tags:

No Responses

Leave a Response

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>