批处理脚本通过CSV文件循环到MySQL

时间:2018-01-16 20:49:13

标签: mysql windows csv batch-file

如何遍历目录中的文件(它们都是CSV文件),然后使用mysql -u root登录mysql(为简单起见,没有密码)并将它们全部添加到mysql表中?这是将文件快速加载到mysql中的查询:

LOAD DATA LOCAL INFILE "C:/prog/mydb_load_Cards_chunks.dump" INTO TABLE vcc2.tmp_cards CHARACTER SET latin1
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES;

如何在登录mysql时对目录中的每个文件执行此操作?

1 个答案:

答案 0 :(得分:0)

尝试:

档案:load_csv.sh (-r-x------ user user load_csv.sh*)

#!/bin/bash

for current_csv in *.csv
do
mysql --login-path=myconnection -e "USE test" -e "
  LOAD DATA LOCAL INFILE '$current_csv'
  INTO TABLE temp_table
  FIELDS TERMINATED BY ','
  OPTIONALLY ENCLOSED BY '\"'
  LINES TERMINATED BY '\n'
  IGNORE 1 LINES
  (DATE, TIME);"
done

请参阅4.6.6 mysql_config_editor — MySQL Configuration Utility

档案:0.csv

DATE,TIME
"2000-01-01","00:00:00"
"2001-01-01","12:00:00"
"2002-01-01","23:00:00"

档案:1.csv

DATE,TIME
"2003-01-01","23:00:00"
"2004-01-01","00:00:00"
"2005-01-01","12:00:00"

档案:2.csv

DATE,TIME
"2006-01-01","12:00:00"
"2007-01-01","23:00:00"
"2008-01-01","00:00:00"

MySQL命令行:

mysql> CREATE DATABASE IF NOT EXISTS `test`;
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> USE `test`;
Database changed

mysql> DROP TABLE IF EXISTS `temp_table`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `temp_table` (
    ->   `id` SERIAL,
    ->   `DATE` DATE,
    ->   `TIME` TIME
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> \! bash
$ ./load_csv.sh
$ exit
exit

mysql> SELECT `id`, `DATE`, `TIME`
    -> FROM `temp_table`;
+----+------------+----------+
| id | DATE       | TIME     |
+----+------------+----------+
|  1 | 2000-01-01 | 00:00:00 |
|  2 | 2001-01-01 | 12:00:00 |
|  3 | 2002-01-01 | 23:00:00 |
|  4 | 2003-01-01 | 23:00:00 |
|  5 | 2004-01-01 | 00:00:00 |
|  6 | 2005-01-01 | 12:00:00 |
|  7 | 2006-01-01 | 12:00:00 |
|  8 | 2007-01-01 | 23:00:00 |
|  9 | 2008-01-01 | 00:00:00 |
+----+------------+----------+
9 rows in set (0.00 sec)