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;
Ïà¹ØÎĵµ£º
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.
  ......
The following is improved version of the code created by David Mullet, from
http://rubyonwindows.blogspot.com/2007/03/ruby-ado-and-sqlserver.html
require 'win32ole'
class SqlServer
# This class manages database connection and queries
attr_accessor :connection, :data, :fields
attr_wr ......
ÁùÖÖÓÃrubyµ÷ÓÃÖ´ÐÐshellÃüÁîµÄ·½·¨
Åöµ½ÐèÒªµ÷ÓòÙ×÷ϵͳshellÃüÁîµÄʱºò£¬RubyΪÎÒÃÇÌṩÁËÁùÖÖÍê³ÉÈÎÎñµÄ·½·¨£º
1.Exec·½·¨:
Kernel#exec·½·¨Í¨¹ýµ÷ÓÃÖ¸¶¨µÄÃüÁîÈ¡´úµ±Ç°½ø³Ì£º
Àý×Ó£º
$ irb
>> exec 'echo " ......
¡¡×î½ü£¬ÎÒÐèÒªÒ»ÖÖ¿ìËÙ¿ª·¢ÓïÑÔ£¬Ä¿Ç°ÖËÊÖ¿ÉÈȵĿìËÙ¿ª·¢ÓïÑÔ¾ÍÊÇPythonºÍRubyÁË¡£RubyºÍPython¶¼ÊǷdz£ÓÅÐãµÄ½Å±¾ÓïÑÔ£¬ËäÈ»ËûÃǵÄÉè¼ÆÕÜѧ¼¸ºõÍêÈ«²»Í¬£¬µ«ËûÃÇЧÂʲ¶à£¬Ó¦ÓÃÁìÓòÒ²²î¾à²»´ó£¬ËƺõÌìÉú¾ÍÊǾºÕù¶ÔÊÖ¡£µ½µ×Ñ¡ÄǸöÄØ?ÎÒ×öÁËһЩ¼òµ¥µÄ±È½Ï¡£
¡¡¡¡ÎÒÃDZȽ ......
ÔÚRuby on RailsÖÐÕæµÄÓÐÒ»¶ÑSelect helper¿ÉÒÔÓÃ,ÎÒÃǾ³£ÈÝÒ×»ìÏý¡£³£¼ûµÄÓÐÈý¸ö..
select, select_tag, collection_select(ÆäÓàµÄʲôselect_dateÄÇЩ²»Ì¸)
ÎÒÃÇÏÈÀ´¿´¿´Ò»¸ö»ù±¾µÄÏÂÀʽѡÏî¹Ç¼Ü
</p>
<select
name="ROR">
<option
value="1">ROR1</option><br
/>
<optio ......