How can I loop through the files in a directory (they are all CSV files), and then log into mysql with mysql -u root
(no password for sake of simplicity) and add them all to a mysql table? This is the query that loads files into mysql fast:
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;
How can I do this for every file in a directory while being logged into mysql?
Try:
File: load_csv.sh (-rx------ 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
See 4.6.6 mysql_config_editor — MySQL Configuration Utility .
File: 0.csv
DATE,TIME
"2000-01-01","00:00:00"
"2001-01-01","12:00:00"
"2002-01-01","23:00:00"
File: 1.csv
DATE,TIME
"2003-01-01","23:00:00"
"2004-01-01","00:00:00"
"2005-01-01","12:00:00"
File: 2.csv
DATE,TIME
"2006-01-01","12:00:00"
"2007-01-01","23:00:00"
"2008-01-01","00:00:00"
MySQL Command-Line:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.