本文で使用したリストです。また、リスト1およびリスト3はリスト下部のリンクをクリックすることで行番号なしのテキストファイルをダウンロードできます。ファイルはシフトJISになっていますので、漢字コードを適宜変更してご利用ください。
1 #!/usr/local/bin/ruby-Ke 2 3 require 'mysql' 4 5 object = Mysql::new('localhost','ユーザー名','パスワード') 6 object.create_db("TEST_RUBY") 7 object.close 8 9 object = Mysql::new('localhost','ユーザー名','パスワード','TEST_RUBY') 10 object.query('create table test_table (name char(32),price int)') 11 object.query("insert into test_table values ('りんご',300);") 12 object.query("insert into test_table values ('みかん',80);") 13 object.query("insert into test_table values ('梨',150);") 14 object.query("insert into test_table values ('桃',250);") 15 object.query("insert into test_table values ('メロン',3450);") 16 object.query("insert into test_table values ('いちご',450);") 17 18 puts object.list_dbs().join(",") 19 puts object.server_info() 20 puts object.host_info() 21 i= object.list_fields('test_table').num_fields() 22 for count in 0..i-1 23 field_object = object.list_fields('test_table').fetch_field_direct(count) 24 puts field_object.name 25 end 26 field_object = object.list_fields('test_table').fetch_field_direct(0) 27 puts field_object.name 28 puts field_object.table 29 puts field_object.def 30 puts field_object.type 31 puts field_object.length 32 33 object.close
<html> <head> <title>Ruby&MySQL</title> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=x-euc-jp"> </head> <body bgcolor="#CCFFCC"> <center> <form action=search.cgi method=post> <table cellspacing="0" cellpadding="2" width=280 bgcolor="#CCFFCC" border="0"> <tr><th bgcolor="#FFCCCC" width=50%>名 前 <td><input type="text" name="name1" size="15"> <tr><th bgcolor="#99FFCC" width=50%>値 段 <td><input type="text" name="price1" size="15"> <td><select name="zyoken1"><option value=1>一致 <option value=2> 以上<option value=3>以下</select> </table> <br> <input type=submit value=" 検 索 "> </center> </body> </html>
1 #!/usr/local/bin/ruby 2 3 print "Content-type: text/html\n\n" 4 require "cgi" 5 require "mysql" 6 7 form = CGI.new 8 9 f_name = form['name1'][0] 10 f_price = form['price1'][0] 11 f_and_or = form['and_or1'][0] 12 f_zyoken = form['zyoken1'][0].to_i 13 14 case f_zyoken 15 when 1 16 zyoken = "=" 17 when 2 18 zyoken = ">=" 19 when 3 20 zyoken = "<=" 21 end 22 23 msql = Mysql::new('192.168.1.10','test','test2001','TEST_RUBY') 24 res_price = msql.query("select name,price from test_table where price #{zyoken} '#{Mysql::quote f_price}'") 25 res_name = msql.query("select name,price from test_table where name='#{Mysql::quote f_name}'") 26 27 if res_price.num_rows == 0 && res_name.num_rows == 0 28 serched_out = "見つかりませんでした" 29 else 30 gokei = res_price.num_rows + res_name.num_rows 31 serched_out = "#{gokei}件見つかりました" 32 end 33 34 35 name_array = [0] 36 price_array = [0] 37 i = 0 38 res_name.each do |name,price| 39 name_array[i] = "#{name}" 40 price_array[i] ="#{price}" 41 i += 1 42 end 43 res_price.each do |name,price| 44 name_array[i] = "#{name}" 45 price_array[i] ="#{price}" 46 i += 1 47 end 48 49 puts '<html><head><title>Ruby&MySQL</title> </head><body bgcolor="#CCFFCC"><center>' 50 puts serched_out 51 puts '<table cellspacing="0" cellpadding="2" width=280 bgcolor="#CCFFCC" border="1">' 52 for count in 0..i-1 53 puts '<tr><th bgcolor="#FFCCCC" width=50%>' + name_array[count] 54 puts '<td bgcolor="white">' + price_array[count] + "円" 55 end 56 puts '</table></body></html>'
Copyright © ITmedia, Inc. All Rights Reserved.