MySQLの最近のブログ記事

MySQLを使う

| コメント(0) | トラックバック(0)
ググっても、役に立ちそうなサイトが殆ど見つからなかったので、とりあえず、
いろいろ試して見ようと思う。

まず、DBの作成とユーザーの作成。
mysql> CREATE DATABASE `depot` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.49 sec)

mysql> GRANT ALL PRIVILEGES ON `depot` . * TO 'depot'@'localhost' WITH GRANT OPTION;
Query OK, 0 rows affected (0.31 sec)

mysql> set password for depot@localhost=password('depot');
Query OK, 0 rows affected (0.13 sec)

mysql> FLUSH PRIVILEGES;

次に、config/database.ymlを編集する。
development:  adapter: mysql2
  encoding: utf8
  database: depot
  pool: 5  username: depot
  password:depot
  socket: /tmp/mysql-1.sock

モデルクラスを作成する。
[taka@centos6 depot]$ rails generate scaffold Product title:string description:text image_url:string price:decimal

なんかエラーがでて怒られたので

[root@centos6 ~]# gem install activerecord-mysql2-adapter

Gemfileの
gem 'sqlite3' を gem 'mysql2' に書き換えて、もう一回
[taka@centos6 depot]$ rails generate scaffold Product title:string description:text image_url:string price:decimal
今度は上手くいった。

次はマイグレーション。
[taka@centos6 depot] $rake db:migrate

テーブルを確認してみよう。
mysql> show create table products;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                                                                             |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| products | CREATE TABLE `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) DEFAULT NULL,
  `description` text,
  `image_url` varchar(255) DEFAULT NULL,
  `price` decimal(8,2) DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

おー テーブルが出来てる。

次はレコードのインサートができるか試して見よう。
http://sabakan.org:3000/products/

products.jpg

レコードのインサートも問題ないようだ。
mysql> select * from products;
+----+-----------------------------------+--------------------------------+-----------+---------+---------------------+---------------------+
| id | title                             | description                    | image_url | price   | created_at          | updated_at          |
+----+-----------------------------------+--------------------------------+-----------+---------+---------------------+---------------------+
|  1 | マグカップ(ともえまみ            | 美しいマグカップです           |           | 1500.00 | 2012-10-07 07:00:17 | 2012-10-07 07:00:17 |
+----+-----------------------------------+--------------------------------+-----------+---------+---------------------+---------------------+
1 row in set (0.00 sec)

app/controllers/products_controller.rbを覗いてみよう。

class ProductsController < ApplicationController
  # GET /products
  # GET /products.json
  def index
    @products = Product.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end

  # GET /products/1
  # GET /products/1.json
  def show
    @product = Product.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @product }
    end
  end

  # GET /products/new
  # GET /products/new.json
  def new
    @product = Product.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @product }
    end
  end

  # GET /products/1/edit
  def edit
    @product = Product.find(params[:id])
  end

  # POST /products
:


ふーん。
へぇー。 なるほど。

このアーカイブについて

このページには、過去に書かれたブログ記事のうちMySQLカテゴリに属しているものが含まれています。

前のカテゴリはinstallです。

次のカテゴリはデモ作成です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。