MySQLにコマンドでCSVファイルからデータをインポート、CSVファイルにエクスポートする

ここに書くのは久しぶりすぎる。。。さーせん。

では、本題。

MySQLをコマンドで扱っていて、CSVファイルのデータをインサートする必要があったので、基本的なことかもしれないけど、メモ程度に書いておきます。

#mysql -u username -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 742
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> load data local infile ファイルパス into table テーブル名 fields terminated by ',';

もし、CSVファイルで文字列が""で囲まれている場合はこちらを使ってください。

mysql> load data local infile ファイルパス into table テーブル名 fields terminated by ','ENCLOSED BY '"';

ついでに、CSVへのエクスポートは

mysql> select * from テーブル名 into outfile ファイル名 fields terminated by ',';