「ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert」の編集履歴(バックアップ)一覧はこちら

ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert」(2012/11/05 (月) 03:04:05) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

* MySqlを使ってみるselect,insert @pagesでは、rubyとMySqlを使えると聞いて手を出してみたので、MySqlの使い方を調べてみました。 - [[MySqlに接続>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#MySqlに接続]] - [[select文の実行>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#select文の実行]] - [[select文の実行結果取得>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#select文の実行結果取得]] - [[insert文の実行>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#insert文の実行]] - [[リクエストパラメータの取得>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#リクエストパラメータの取得]] - [[リダイレクト>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#リダイレクト]] - [[サンプルソース(mysql.cgi)>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#サンプルソースmysql.cgi]] - [[サンプルソース(mysql.insert.cgi)>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#サンプルソースmysql.insert.cgi]] - [[サンプルダウンロード>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#サンプルダウンロード]] &aname(MySqlに接続) * MySqlに接続 @pagesでMySqlに接続するには、ユーザー名、パスワード、ホスト名、データベース名を管理画面から取得し、 以下のようにMysql::newに設定します。 #highlight(ruby){{ # DB接続 require 'mysql' user = 'ユーザー名' pass = 'パスワード' host = 'localhost' name = 'データベース名' db = Mysql::new(host, user, pass, name ) }} &aname(select文の実行) * select文の実行 パラメータのないselect文なら、queryメソッドでselect文を実行し、Mysql::Resultオブジェクトを取得できます。 #highlight(ruby){{ # クエリ実行 res = db.query("select * from TestTable") }} &aname(select文の実行結果取得) * select文の実行結果取得 select文の実行結果は、eachメソッドでカラムの配列として取得できます。 #highlight(ruby){{ # データ取得&出力 res.each do |row| col1 = row[0] col2 = row[1] col3 = row[2] col4 = row[3] end }} &aname(insert文の実行) * insert文の実行 insert文はパラメータが基本必要なので、Mysql#prepareメソッドを使い、Mysql::Stmtオブジェクトを取得します。 Mysql::Stmt#executeメソッドにパラメータを渡すと、クエリの ? にパラメータが設定されます。 select文のパラメータを設定する場合も、同じ方法でよいと思います。 #highlight(ruby){{ # クエリ実行 st = db.prepare("insert into TestTable (name, birth, address, email) values (?,?,?,?)") st.execute(name, birth, address, email) st.close() db.close() }} &aname(リクエストパラメータの取得) * リクエストパラメータの取得 フォームから送信されたリクエストパラメータの取得はCGI#paramsメソッドを使いHashを取得します。 #highlight(ruby){{ # リクエストパラメータ取得 require 'cgi' cgi = CGI.new name = cgi["name"] birth = cgi["birth"] address = cgi["address"] email = cgi["email"] }} &aname(リダイレクト) * リダイレクト insert後はまた初期画面に戻りたいので、Ggi#headerメソッドを使い標準出力に結果を出力します。 #highlight(ruby){{ # mysql.cgiへリダイレクト print cgi.header({'status' => '302 Found', 'Location' => 'mysql.cgi' }) }} &aname(サンプルソースmysql.cgi) * サンプルソース(mysql.cgi) #highlight(ruby){{ #!/usr/local/bin/ruby # コンテンツタイプ出力 print "Content-type: text/html\n\n" # ヘッダ出力 print(<<"head") <html> <head> <title>ruby and MySql - ちゃぱてぃ商店@pages</title> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> </head> head # DB接続 require 'mysql' user = 'chapati' pass = 'ぱすわーど' host = 'localhost' name = 'db0chapati' db = Mysql::new(host, user, pass, name ) # クエリ実行 res = db.query("select * from TestTable") # データ取得&出力 res.each do |row| col1 = row[0] col2 = row[1] col3 = row[2] col4 = row[3] puts col1 puts ", " puts col2 puts ", " puts col3 puts ", " puts col4 puts "<br>" end db.close() # フォーム出力 print(<<"form") <form action='mysql.insert.cgi'> <label>名前:</label> <input type='text' id='name' name='name' /><br> <label>生年月日:</label> <input type='text' id='birth' name='birth' /><br> <label>住所:</label> <input type='text' id='address' name='address' /><br> <label>メールアドレス:</label> <input type='text' id='email' name='email' /><br> <input type='submit'> </form> form print "</html>" }} &aname(サンプルソースmysql.insert.cgi) * サンプルソース(mysql.insert.cgi) #highlight(ruby){{ #!/usr/local/bin/ruby # リクエストパラメータ取得 require 'cgi' cgi = CGI.new name = cgi["name"] birth = cgi["birth"] address = cgi["address"] email = cgi["email"] # DB接続 require 'mysql' user = 'ユーザー名' pass = 'ぱすわーど' host = 'localhost' dbname = 'db0chapati' db = Mysql::new(host, user, pass, dbname ) # クエリ実行 st = db.prepare("insert into TestTable (name, birth, address, email) values (?,?,?,?)") st.execute(name, birth, address, email) st.close() db.close() # mysql.cgiへリダイレクト print cgi.header({'status' => '302 Found', 'Location' => 'mysql.cgi' }) }}
* MySqlを使ってみるselect,insert @pagesでは、rubyとMySqlを使えると聞いて手を出してみたので、MySqlの使い方を調べてみました。 - [[サンプルダウンロード>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#サンプルダウンロード]] - [[MySqlに接続>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#MySqlに接続]] - [[select文の実行>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#select文の実行]] - [[select文の実行結果取得>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#select文の実行結果取得]] - [[insert文の実行>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#insert文の実行]] - [[リクエストパラメータの取得>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#リクエストパラメータの取得]] - [[リダイレクト>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#リダイレクト]] - [[サンプルソース(mysql.cgi)>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#サンプルソースmysql.cgi]] - [[サンプルソース(mysql.insert.cgi)>ruby/サンプル/@pagesでruby/MySqlを使ってみるselect,insert#サンプルソースmysql.insert.cgi]] &aname(サンプルダウンロード) * サンプルダウンロード &ref(mysql.cgi) &ref(mysql.insert.cgi) &aname(MySqlに接続) * MySqlに接続 @pagesでMySqlに接続するには、ユーザー名、パスワード、ホスト名、データベース名を管理画面から取得し、 以下のようにMysql::newに設定します。 #highlight(ruby){{ # DB接続 require 'mysql' user = 'ユーザー名' pass = 'パスワード' host = 'localhost' name = 'データベース名' db = Mysql::new(host, user, pass, name ) }} &aname(select文の実行) * select文の実行 パラメータのないselect文なら、queryメソッドでselect文を実行し、Mysql::Resultオブジェクトを取得できます。 #highlight(ruby){{ # クエリ実行 res = db.query("select * from TestTable") }} &aname(select文の実行結果取得) * select文の実行結果取得 select文の実行結果は、eachメソッドでカラムの配列として取得できます。 #highlight(ruby){{ # データ取得&出力 res.each do |row| col1 = row[0] col2 = row[1] col3 = row[2] col4 = row[3] end }} &aname(insert文の実行) * insert文の実行 insert文はパラメータが基本必要なので、Mysql#prepareメソッドを使い、Mysql::Stmtオブジェクトを取得します。 Mysql::Stmt#executeメソッドにパラメータを渡すと、クエリの ? にパラメータが設定されます。 select文のパラメータを設定する場合も、同じ方法でよいと思います。 #highlight(ruby){{ # クエリ実行 st = db.prepare("insert into TestTable (name, birth, address, email) values (?,?,?,?)") st.execute(name, birth, address, email) st.close() db.close() }} &aname(リクエストパラメータの取得) * リクエストパラメータの取得 フォームから送信されたリクエストパラメータの取得はCGI#paramsメソッドを使いHashを取得します。 #highlight(ruby){{ # リクエストパラメータ取得 require 'cgi' cgi = CGI.new name = cgi["name"] birth = cgi["birth"] address = cgi["address"] email = cgi["email"] }} &aname(リダイレクト) * リダイレクト insert後はまた初期画面に戻りたいので、Ggi#headerメソッドを使い標準出力に結果を出力します。 #highlight(ruby){{ # mysql.cgiへリダイレクト print cgi.header({'status' => '302 Found', 'Location' => 'mysql.cgi' }) }} &aname(サンプルソースmysql.cgi) * サンプルソース(mysql.cgi) 「TestTable」のデータを全て読み込んで画面に表示し、新しいデータの入力フォームを表示します。 入力フォームはmysql.insert.cgiに送信します。 #highlight(ruby){{ #!/usr/local/bin/ruby # コンテンツタイプ出力 print "Content-type: text/html\n\n" # ヘッダ出力 print(<<"head") <html> <head> <title>ruby and MySql - ちゃぱてぃ商店@pages</title> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> </head> head # DB接続 require 'mysql' user = 'chapati' pass = 'ぱすわーど' host = 'localhost' name = 'db0chapati' db = Mysql::new(host, user, pass, name ) # クエリ実行 res = db.query("select * from TestTable") # データ取得&出力 res.each do |row| col1 = row[0] col2 = row[1] col3 = row[2] col4 = row[3] puts col1 puts ", " puts col2 puts ", " puts col3 puts ", " puts col4 puts "<br>" end db.close() # フォーム出力 print(<<"form") <form action='mysql.insert.cgi'> <label>名前:</label> <input type='text' id='name' name='name' /><br> <label>生年月日:</label> <input type='text' id='birth' name='birth' /><br> <label>住所:</label> <input type='text' id='address' name='address' /><br> <label>メールアドレス:</label> <input type='text' id='email' name='email' /><br> <input type='submit'> </form> form print "</html>" }} &aname(サンプルソースmysql.insert.cgi) * サンプルソース(mysql.insert.cgi) mysql.cgiからリクエストパラメータを受け取り、「TestTable」に新しいレコードを作成するサンプルです。 新しいレコードの作成後は「mysql.cgi」にリダイレクトします。 #highlight(ruby){{ #!/usr/local/bin/ruby # リクエストパラメータ取得 require 'cgi' cgi = CGI.new name = cgi["name"] birth = cgi["birth"] address = cgi["address"] email = cgi["email"] # DB接続 require 'mysql' user = 'ユーザー名' pass = 'ぱすわーど' host = 'localhost' dbname = 'db0chapati' db = Mysql::new(host, user, pass, dbname ) # クエリ実行 st = db.prepare("insert into TestTable (name, birth, address, email) values (?,?,?,?)") st.execute(name, birth, address, email) st.close() db.close() # mysql.cgiへリダイレクト print cgi.header({'status' => '302 Found', 'Location' => 'mysql.cgi' }) }}

表示オプション

横に並べて表示:
変化行の前後のみ表示: