blog/content/post/2009-07-07-00001193.md

123 lines
10 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
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
---
<div class="section">
<p>
スクレイピングした結果をデータベースに格納してみようと思い立ちました。というわけで、MySQLを触ることに。
</p>
<h4>
サンプルのデータベース・テーブルの作り方
</h4>
<pre class="syntax-highlight">
kazu634@kazu634-desktop% mysql <span class="synSpecial">-u</span> root <span class="synSpecial">-p</span> ~ <span class="synStatement">[</span><span class="synConstant">2961</span><span class="synStatement">]</span>
Enter password:
Welcome to the MySQL monitor. Commands end with <span class="synStatement">;</span> or \g.
Your MySQL connection id is <span class="synConstant">45</span>
Server version: <span class="synConstant">5</span>.<span class="synConstant"></span>.<span class="synConstant">75</span>-0ubuntu10.<span class="synConstant">2</span> <span class="synStatement">(</span>Ubuntu<span class="synStatement">)</span>
Type <span class="synStatement">'</span><span class="synConstant">help;</span><span class="synStatement">'</span> or <span class="synStatement">'</span><span class="synConstant">\h</span><span class="synStatement">'</span> <span class="synStatement">for</span> <span class="synStatement">help</span>. Type <span class="synStatement">'</span><span class="synConstant">\c</span><span class="synStatement">'</span> to <span class="synStatement">clear</span> the buffer.
mysql<span class="synStatement">&#62;</span> show databases<span class="synStatement">;</span>
+--------------------+
<span class="synStatement">|</span> Database <span class="synStatement">|</span>
+--------------------+
<span class="synStatement">|</span> information_schema <span class="synStatement">|</span>
<span class="synStatement">|</span> mysql <span class="synStatement">|</span>
+--------------------+
<span class="synConstant">2</span> rows <span class="synStatement">in</span> <span class="synStatement">set</span> <span class="synStatement">(</span><span class="synConstant"></span>.<span class="synConstant">01</span> sec<span class="synStatement">)</span>
mysql<span class="synStatement">&#62;</span> create database sample<span class="synStatement">;</span>
Query OK, <span class="synConstant">1</span> row affected <span class="synStatement">(</span><span class="synConstant"></span>.<span class="synConstant">00</span> sec<span class="synStatement">)</span>
mysql<span class="synStatement">&#62;</span> show databases<span class="synStatement">;</span>
+--------------------+
<span class="synStatement">|</span> Database <span class="synStatement">|</span>
+--------------------+
<span class="synStatement">|</span> information_schema <span class="synStatement">|</span>
<span class="synStatement">|</span> mysql <span class="synStatement">|</span>
<span class="synStatement">|</span> sample <span class="synStatement">|</span>
+--------------------+
<span class="synConstant">3</span> rows <span class="synError">in</span> <span class="synStatement">set</span> <span class="synStatement">(</span><span class="synConstant"></span>.<span class="synConstant">00</span> sec<span class="synStatement">)</span>
mysql<span class="synStatement">&#62;</span> USE sample
Database changed
mysql<span class="synStatement">&#62;</span> SHOW TABLES<span class="synStatement">;</span>
Empty <span class="synStatement">set</span> <span class="synStatement">(</span><span class="synConstant"></span>.<span class="synConstant">00</span> sec<span class="synStatement">)</span>
mysql<span class="synStatement">&#62;</span> CREATE TABLE Prefecture<span class="synStatement">(</span>
-<span class="synStatement">&#62;</span> PREF_CD INT<span class="synStatement">(</span><span class="synConstant">3</span><span class="synStatement">)</span>,
-<span class="synStatement">&#62;</span> PREF_NAME VARCHAR<span class="synStatement">(</span><span class="synConstant">10</span><span class="synStatement">)</span>,
-<span class="synStatement">&#62;</span> PRIMARY KEY <span class="synStatement">(</span>PREF_CD<span class="synStatement">)</span>
-<span class="synStatement">&#62;</span> <span class="synStatement">);</span>
Query OK, <span class="synConstant"></span> rows affected <span class="synStatement">(</span><span class="synConstant"></span>.<span class="synConstant">01</span> sec<span class="synStatement">)</span>
mysql<span class="synStatement">&#62;</span> SHOW TABLES+
-<span class="synStatement">&#62;</span> <span class="synStatement">;</span>
ERROR <span class="synConstant">1064</span> <span class="synStatement">(</span><span class="synConstant">42000</span><span class="synStatement">)</span>: You have an error <span class="synError">in</span> your SQL syntax<span class="synStatement">;</span> check the manual that corresponds to your MySQL server version <span class="synStatement">for</span> the right syntax to use near <span class="synStatement">'</span><span class="synConstant">+</span><span class="synStatement">'</span> at line <span class="synConstant">1</span>
mysql<span class="synStatement">&#62;</span> SHOW TABLES<span class="synStatement">;</span>
+------------------+
<span class="synStatement">|</span> Tables_in_sample <span class="synStatement">|</span>
+------------------+
<span class="synStatement">|</span> Prefecture <span class="synStatement">|</span>
+------------------+
<span class="synConstant">1</span> row <span class="synStatement">in</span> <span class="synStatement">set</span> <span class="synStatement">(</span><span class="synConstant"></span>.<span class="synConstant">00</span> sec<span class="synStatement">)</span>
mysql<span class="synStatement">&#62;</span> DESC Prefecture
-<span class="synStatement">&#62;</span> <span class="synStatement">;</span>
+-----------+-------------+------+-----+---------+-------+
<span class="synStatement">|</span> Field <span class="synStatement">|</span> Type <span class="synStatement">|</span> Null <span class="synStatement">|</span> Key <span class="synStatement">|</span> Default <span class="synStatement">|</span> Extra <span class="synStatement">|</span>
+-----------+-------------+------+-----+---------+-------+
<span class="synStatement">|</span> PREF_CD <span class="synStatement">|</span> int<span class="synStatement">(</span><span class="synConstant">3</span><span class="synStatement">)</span> <span class="synStatement">|</span> NO <span class="synStatement">|</span> PRI <span class="synStatement">|</span> <span class="synConstant"></span> <span class="synStatement">|</span> <span class="synStatement">|</span>
<span class="synStatement">|</span> PREF_NAME <span class="synStatement">|</span> varchar<span class="synStatement">(</span><span class="synConstant">10</span><span class="synStatement">)</span> <span class="synStatement">|</span> YES <span class="synStatement">|</span> <span class="synStatement">|</span> NULL <span class="synStatement">|</span> <span class="synStatement">|</span>
+-----------+-------------+------+-----+---------+-------+
<span class="synConstant">2</span> rows <span class="synError">in</span> <span class="synStatement">set</span> <span class="synStatement">(</span><span class="synConstant"></span>.<span class="synConstant">01</span> sec<span class="synStatement">)</span>
mysql<span class="synStatement">&#62;</span> <span class="synStatement">exit;</span>
Bye
</pre>
<h4>
次は
</h4>
<p>
<a href="http://d.hatena.ne.jp/asin/4798119172" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4798119172', 'モダンPerl入門 (CodeZine BOOKS)');">モダンPerl入門 (CodeZine BOOKS)</a>』でデータベースを使うための方法を確認するぞ。
</p>
<div class="hatena-asin-detail">
<a href="http://www.amazon.co.jp/dp/4798119172/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4798119172/?tag=hatena_st1-22&ascsubtag=d-7ibv', '');"><img src="https://images-na.ssl-images-amazon.com/images/I/41W6wTHDSzL._SL160_.jpg" class="hatena-asin-detail-image" alt="モダンPerl入門 (CodeZine BOOKS)" title="モダンPerl入門 (CodeZine BOOKS)" /></a></p>
<div class="hatena-asin-detail-info">
<p class="hatena-asin-detail-title">
<a href="http://www.amazon.co.jp/dp/4798119172/?tag=hatena_st1-22&ascsubtag=d-7ibv" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://www.amazon.co.jp/dp/4798119172/?tag=hatena_st1-22&ascsubtag=d-7ibv', 'モダンPerl入門 (CodeZine BOOKS)');">モダンPerl入門 (CodeZine BOOKS)</a>
</p>
<ul>
<li>
<span class="hatena-asin-detail-label">作者:</span> <a href="http://d.hatena.ne.jp/keyword/%CB%D2%C2%E7%CA%E5" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%CB%D2%C2%E7%CA%E5', '牧大輔');" class="keyword">牧大輔</a>
</li>
<li>
<span class="hatena-asin-detail-label">出版社/メーカー:</span> <a href="http://d.hatena.ne.jp/keyword/%E6%C6%B1%CB%BC%D2" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/keyword/%E6%C6%B1%CB%BC%D2', '翔泳社');" class="keyword">翔泳社</a>
</li>
<li>
<span class="hatena-asin-detail-label">発売日:</span> 2009/02/10
</li>
<li>
<span class="hatena-asin-detail-label">メディア:</span> 大型本
</li>
<li>
<span class="hatena-asin-detail-label">購入</span>: 25人 <span class="hatena-asin-detail-label">クリック</span>: 534回
</li>
<li>
<a href="http://d.hatena.ne.jp/asin/4798119172" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/4798119172', 'この商品を含むブログ (113件) を見る');" target="_blank">この商品を含むブログ (113件) を見る</a>
</li>
</ul>
</div>
<div class="hatena-asin-detail-foot">
</div>
</div>
</div>