MySQLメモ
MySQL4.X 系はビューがないという致命的な問題がありますが、Catalyst のモデルのヘルパーで PostgreSQL が全く使えないため、MySQL の勉強をはじめました(LAMP にも PostgreSQL は入っていませんし・・)。
MySQL のインストール
apt-get install MySQL-client MySQL-shared MySQL-server MySQL-Max perl-DBD-MySQL
cpan install Class::DBI::mysql
root のパスワード設定
mysqladmin -u root password 'hogehoge'
データベース test を作成
mysqladmin -u root -p create test
データベースにログイン
mysql -u root -p
ユーザー foo の作成
grant all on test.* to foo@localhost identified by 'bar';
flush privileges
quit
ユーザー foo でログイン
mysql -u foo -p test
テーブルの作成
create table user_master (
id int not null primary key auto_increment,
name text not null,
login_id text not null,
login_password text not null
);
データの挿入
insert into user_master (name, login_id, login_password) values ('佐藤次郎', 'satou', 'hogehoge');
insert into user_master (name, login_id, login_password) values ('鈴木一郎', 'suzuki', 'hogehoge');
検索のテスト
cd ../lib/Controller
vi Hello.pm
sub default : Private { my ( $self, $c ) = @_; my @users = Test::Model::CDBI::UserMaster->retrieve_all(); my $body = ''; for my $user (@users) { $user->name(); $body .= Dumper $user; } $c->response->body($body); } sub tt : Local { my ( $self, $c) = @_; my @users = Test::Model::CDBI::UserMaster->retrieve_all(); $c->stash->{users} = \@users; $c->stash->{template} = 'template/hello.html'; $c->forward('Test::View::TT'); }
cd ../../../root/template/hello.html
vi hello.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head><title>[% c.config.name %]</title></head> <body> <h1>ユーザー覧</h1> <table border="1"> <tr> <th>ID</th> <th>名前</th> <th>ログインID</th> <th>ログインパスワード</th> </tr> [% FOREACH user = c.stash.users %] <tr> <td>[% user.id %]</td> <td>[% user.name %]</td> <td>[% user.login_id %]</td> <td>[% user.login_password %]</td> </tr> [% END %] </table> </body> </html>