ruby Mqsql驱动和测试Mysql脚本

http://rubyforge.org/projects/mysql-win ,下载 mysql-2.7.3-mswin32.gem ,这是 mysql_Ruby驱动程序。

命令行,进入该文件所在目录,运行 gem install mysql-2.7.3-mswin32.gem ,安装成功即可。

  你也可以在 mysql官方网站下载 ruby的驱动,该网站附带安装步骤,API文档。


require "mysql"   
begin   
  dbh = Mysql.real_connect("localhost", "testUser", "testPassword", "testDB")
  
  dbh.query("drop table if exists test_rb") 
  dbh.query("create table test_rb(id int,name varchar(20))") 
  dbh.query("insert into test_rb values(1,'ss')") 
  dbh.query("insert into test_rb values(1,'aaa')") 
  printf "%d rows were inserted\n",dbh.affected_rows 
  
  res=dbh.query("SELECT name FROM test_rb") 
  puts "===============\n" 
  while row=res.fetch_row do 
  printf "%s,%s\n",row[0],row[1] 
  end 
  puts "================\n" 
  puts "Server version:"+dbh.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 
  dbh.close if dbh 
end 


加一段连接SQL的
用SQL SERVER数据库连接RUBY ON RAILS解决乱码问题

Rails 连接 Sql Server的问题

Rails 连接 Sql Server 小结

require "dbi"
dbh=DBI.connect('DBI:ADO:Provider=SQLOLEDB;Connect Timeout=5;DataSource=localhost;Initial Catalog=SmileDemo;Persist SecurityInfo=True;User ID=ruby;Password=ruby;')
sth=dbh.prepare('select * from users')
sth.execute
while row=sth.fetch do
p row
end


development: 
adapter: sqlserver 
database: mybook 
username: sa 
host: DBI:ADO:Provider=SQLOLEDB;Data Source="127.0.0.1";Initial Catalog="mybook";User Id="sa";password="123"; 
password: 123 

你可能感兴趣的:(mysql,SQL Server,脚本,Ruby,Rails)