--- title: MySQLを触ってみた author: kazu634 date: 2009-07-07 url: /2009/07/07/_1291/ wordtwit_post_info: - 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4697;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - database - mysql ---

スクレイピングした結果をデータベースに格納してみようと思い立ちました。というわけで、MySQLを触ることに。

サンプルのデータベース・テーブルの作り方

kazu634@kazu634-desktop% mysql -u root -p                                                  ~ [2961]
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 45
Server version: 5..75-0ubuntu10.2 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (.01 sec)
mysql> create database sample;
Query OK, 1 row affected (.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| sample             |
+--------------------+
3 rows in set (.00 sec)
mysql> USE sample
Database changed
mysql> SHOW TABLES;
Empty set (.00 sec)
mysql> CREATE TABLE Prefecture(
-> PREF_CD INT(3),
-> PREF_NAME VARCHAR(10),
-> PRIMARY KEY (PREF_CD)
-> );
Query OK,  rows affected (.01 sec)
mysql> SHOW TABLES+
-> ;
ERROR 1064 (42000): 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 '+' at line 1
mysql> SHOW TABLES;
+------------------+
| Tables_in_sample |
+------------------+
| Prefecture       |
+------------------+
1 row in set (.00 sec)
mysql> DESC Prefecture
-> ;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| PREF_CD   | int(3)      | NO   | PRI |        |       |
| PREF_NAME | varchar(10) | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (.01 sec)
mysql> exit;
Bye

次は

モダンPerl入門 (CodeZine BOOKS)』でデータベースを使うための方法を確認するぞ。

モダンPerl入門 (CodeZine BOOKS)

モダンPerl入門 (CodeZine BOOKS)