Skip to content

Instantly share code, notes, and snippets.

@TheDefinitionist
Last active December 6, 2016 19:31
Show Gist options
  • Save TheDefinitionist/aaa8b12cb94d5e910075732bf6a3a7aa to your computer and use it in GitHub Desktop.
Save TheDefinitionist/aaa8b12cb94d5e910075732bf6a3a7aa to your computer and use it in GitHub Desktop.
Imports CSV data into MySQL and exports it again
-- 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