Ruby实践—连接Mysql
安装环境:
OS:Windows XP
Ruby: Ruby1.9.1
Mysql: Mysql5.1.46 (username/password: root/root port:3306)
Ruby-Mysql Driver: mysql-2.8.1-x86-mswin32.gem
(注:用2.7.3版本的驱动在测试时会出现 require"mysql",找不到指定模块 错误)
IDE:RubyMine2.0.1
安装Ruby,RubyMine,Mysql的事项在这里就不多说了,提一下安装驱动的步骤
1)在Mysql安装目录的 bin 目录下,找到 ibmySQL.dll ,将该文件复制到 Ruby 安装目录的 bin 目录中
2)http://rubyforge.org/frs/?group_id=1598,下载 mysql-2.8.1-x86-mswin32.gem,这是 mysql_Ruby驱动程序。
命令行,进入该文件所在目录,运行 gem install mysql-2.8.1-x86-mswin32.gem ,安装成功即可。
创建Ruby测试类
MysqlTest.rb
class MysqlTest
#Code here
require "mysql"
def testMysql
dbc=Mysql.real_connect('localhost','root','root','mysql')
res=dbc.query('select * from user')
puts "Test Mysql...."
while row=res.fetch_row do
puts "#{row[0]}"
end
end
def createTable
dbc=Mysql.real_connect('localhost','root','root','test')
dbc.query("drop table if exists cux_users")
dbc.query("create table cux_users(id int,name char(20))")
dbc.query("insert into cux_users values(1,'Tom')")
printf "Create Table........"
printf "%d rows were inserted\n",dbc.affected_rows
res = dbc.query("SELECT name from cux_users")
puts "===Select Data===\n"
while row = res.fetch_row do
printf "%s, %s\n", row[0], row[1]
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
(MysqlTest.new).testMysql
(MysqlTest.new).createTable
end
测试结果:
C:\Ruby19\bin\ruby.exe -e STDOUT.sync=true;
相关文档:
喜欢玩阿月系列RPG游戏的我,跟不少童鞋一样下了RPGMakerXP来瞻仰一下。当时觉得这个软件太方便了,用鼠标随便点点就能弄出来个有趣的小游戏。因为里面不仅自带不少地图和人物行走图等素材,容易上手的操作方式也让自己大有成就感……可是游戏毕竟是离不开编程,解开游戏包,看 ......
To get it done is not easy. I spent a whole day to figure out the various compatibility issues along the way out.
Now there still might be potential issues, but it works by my rough test.
Step 1: Install Apache Cassandra
You may know that the Ruby gem cassandra will do it for you.
  ......
转 Adding Sound to Your Ruby Apps
Have you ever thought about including sounds in your Ruby application? Used sparingly, sound may enhance your applications by adding audio cues or a custom touch. You could, for example, play a beep or chime that announces the completion of a lengthy process. Per ......
Ruby和Python的语法比较
其实Ruby和Python非常接近,比大多数别的语言要接近的多,所以喜欢用啥就用啥(大实话,虽然也是废话)。语法上的差别虽然有那么一点,大部分是syntax sugar,我斗胆稍微列几个(python我也忘得差不多了,不对的大家尽管来鞭尸吧),但是主要差异还是设计思想上的:灵活 ......
1.1.Rails
1.1 创建一个Rails应用程序
$ rails app_name
可选项:
-d, database=xxx 指定安装一个数据库(mysql oracle postgresql sqlite2 sqlite3 ), 默认情况下是数据库
-r, ruby-path= 指定Ruby的安装路径,如果没有指定,scripts使用env去找Ruby
-f, freeze (冻结)freezes Rails在vendor/rails目录
1.2 API Docume ......