Last active
December 6, 2016 19:31
-
-
Save TheDefinitionist/aaa8b12cb94d5e910075732bf6a3a7aa to your computer and use it in GitHub Desktop.
Imports CSV data into MySQL and exports it again
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- import | |
CREATE DATABASE IF NOT EXISTS import_csv; | |
USE import_csv; | |
CREATE TABLE IF NOT EXISTS persons ( | |
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, | |
`firstname` TEXT NOT NULL, | |
`lastname` TEXT NOT NULL, | |
`age` INT NOT NULL | |
); | |
LOAD DATA INFILE 'data.csv' | |
INTO TABLE import_csv.persons | |
FIELDS | |
TERMINATED BY ',' | |
ENCLOSED BY '"' | |
LINES TERMINATED BY '\n' | |
IGNORE 1 ROWS; | |
-- export | |
SELECT `id`, `firstname`, `lastname`, `age` | |
INTO OUTFILE 'data.csv' | |
FIELDS | |
TERMINATED BY ',' | |
OPTIONALLY ENCLOSED BY '"' | |
ESCAPED BY '\\' | |
LINES TERMINATED BY '\n' | |
FROM persons; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment