2013年3月4日月曜日

StrutsによるWebアプリケーションスーパーサンプルのDB作成2

◆概要

文字コードの設定の差異でこける。
インストールしていたMySQLのデフォルトcharsetはutf8。
でもサンプルで想定されていたのはsjhiftjisだったため、
データをloadできなかったり、select結果が化けたりしました。

◆調べた結果
DB作成時に character set = sjisとすればそのDBのcharsetを変えることができる。

create database if not exists struts character set = sjis;

select結果が化ける場合は、
表示したいcharset に変えてあげればよい。

charset sjis;

[select実行結果]
mysql> select * from room
    -> ;
+--------+--------------------+------------+
| roomid | roomNo             | buildingid |
+--------+--------------------+------------+
| 1-201  | 201莨夊ュー螳、          |          1 |
| 1-301  | 301迚ケ蛻・莨夊ュー螳、      |          1 |
| 2-201  | 2F蠢懈磁螳、           |          2 |
| 2-202  | 201莨夊ュー螳、          |          2 |
| 2-203  | 202莨夊ュー螳、          |          2 |
+--------+--------------------+------------+
5 rows in set (0.00 sec)

mysql> charset sjis
Charset changed
mysql> select * from room;
+--------+---------------+------------+
| roomid | roomNo        | buildingid |
+--------+---------------+------------+
| 1-201  | 201会議室     |          1 |
| 1-301  | 301特別会議室 |          1 |
| 2-201  | 2F応接室      |          2 |
| 2-202  | 201会議室     |          2 |
| 2-203  | 202会議室     |          2 |
+--------+---------------+------------+
5 rows in set (0.00 sec)


◆参考資料
http://dev.mysql.com/doc/refman/5.5/en/create-database.html

StrutsによるWebアプリケーションスーパーサンプルのDB作成1

◆概要

Strutsの復習を目的に図書館で本を借りました。
タイトルの本ですが、DB設定で嵌ったのでメモ。

◆調べた結果

MySQLのバージョン差異があったので、
添付されていたcreate tableがうまく動いてくれなかった。

TYPEが使用できないので、TYPE=INNODB ⇒ ENGINE=InnoDB とする。
予約語を避けるようにした。
※今回は「range」 が予約語になっていた。

◆MySQLのバージョン

書籍 ⇒ 5.0.27
ローカルマシン ⇒ 5.5.28

◆エラー内容

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TYPE=INNODB' at line 1

ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range (rangeno INTEGER PRIMARY KEY,timename varchar(5),colspan INTEGER, INDEX (r' at line 1

◆だめだった原因

1.
 TYPE=InnoDB の "TYPE"は5.5では使用できなくなっていた。
 代わりにENGINE=InnoDBを使用する必要がある。

2.
 テーブル名で指定されていた"range" が予約語になっていた。

◆感想

環境回りの前提となるものは先に確認しておく。

◆参考URL

http://dev.mysql.com/doc/refman/5.0/en/create-table.html
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

http://dev.mysql.com/doc/refman/5.5/en/create-table.html
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html