一、確保mysql驅動都已經安裝好。mysql
安裝mysql驅動 gem install mysqlsql
安裝mysql2驅動 gem install mysql2數據庫
二、libmysql.dll 文件是MySQL的動態連接庫文件,缺乏此文件數據庫沒法正常工做。將這個文件 拷貝到 Ruby/Bin目錄中,或者C:\windows\system32目錄也可。windows
libmysql.dll 下載地址: http://www.mysql.com/downloads/connector/c/ruby
使用mysql 驅動fetch
1
2
3
4
5
6
|
require
"mysql"
dbc = Mysql.real_connect(
'主機IP地址'
,
'帳戶名'
,
'密碼'
,
'數據庫名字'
)
res = dbc.query(
'select * from 你的數據表'
)
while
row = res.fetch_row
do
puts
"#{row[0]},#{row[1]}"
end
|
使用mysql2 驅動ui
1
2
3
4
5
6
|
require
'mysql2'
client = Mysql2::Client.
new
(
:host
=>
"主機IP地址"
,
:username
=>
"帳戶名"
,
:password
=>
"密碼"
,
:database
=>
"數據庫名字"
)
results = client.query(
"select * from 你的數據表"
);
results.
each
do
|hash|
puts hash.map { |k,v|
"#{k} = #{v}"
}.join(
", "
)
end
|