Learn how to import and export CSV files in MySQL

Source: Internet
Author: User

 

A sample SQL statement for exporting CSV data in MySQL is as follows:

SQL code
  1. Select * From test_info
  2. Into OUTFILE '/tmp/test.csv'
  3. Fields terminated by ', 'optionally enclosed by' "'escaped '"'
  4. Lines terminated by '\ r \ n ';
Select * From test_info into OUTFILE '/tmp/test.csv' fields terminated by ', 'optionally enclosed by' "'escaped by '" 'Lines terminated by' \ r \ n ';

A sample SQL statement for importing CSV data in MySQL is as follows:

SQL code
  1. Load data infile '/tmp/test.csv'
  2. Into Table test_info
  3. Fields terminated by ', 'optionally enclosed by' "'escaped '"'
  4. Lines terminated by '\ r \ n ';
Load data infile '/tmp/test.csv' into Table test_info fields terminated by ', 'optionally enclosed by' "'fig by '" 'Lines terminated by' \ r \ n ';

The most important part is the format parameter.

SQL code
  1. Fields terminated by ', 'optionally enclosed by' "'escaped '"'
  2. Lines terminated by '\ r \ N'
Fields terminated by ', 'optionally enclosed by' "'elasticed by '" 'Lines terminated by' \ r \ N'

This parameter is set according to rfc4180. The full name of this document is common format and Mime Type for comma-separated values (CSV) files. The CSV format is described in detail. The key points include:

(1) fields are separated by commas, and data rows are separated by \ r \ n;

(2) The string is enclosed by double quotation marks, and the double quotation marks of the string itself are represented by two double quotation marks.

 

File: test_csv. SQL

SQL code
  1. Use test;
  2. Create Table test_info (
  3. Id integer not null,
  4. Content varchar (64) not null,
  5. Primary Key (ID)
  6. );
  7. Delete from test_info;
  8. Insert into test_info values (2010, 'Hello, line
  9. Suped
  10. SEPED
  11. "
  12. End'
  13. );
  14. Select * From test_info;
  15. Select * From test_info into OUTFILE '/tmp/test.csv' fields terminated by ', 'optionally enclosed by' "'escaped by '" 'Lines terminated by' \ r \ n ';
  16. Delete from test_info;
  17. Load data infile '/tmp/test.csv' into Table test_info fields terminated by ', 'optionally enclosed by' "'fig by '" 'Lines terminated by' \ r \ n ';
  18. Select * From test_info;
Use test; Create Table test_info (ID integernot null, content varchar (64) not null, primary key (ID); Delete from test_info; insert into test_info values (2010, 'Hello, linesupedseped "end'); select * From test_info into OUTFILE '/tmp/test.csv 'fields terminated ', 'optionally enclosed by '"'elasticed by'" 'Lines terminated by '\ r \ n'; delete from test_info; load data infile '/tmp/test.csv' into Table test_info fields terminated by ', 'optionally enclosed by' "'fig by '" 'Lines terminated by' \ r \ n '; select * From test_info;

 

File: test.csv

Text code
  1. 2010, "Hello, line
  2. Suped
  3. SEPED
  4. ""
  5. End"
2010, "Hello, linesupedseped" "end"

 

In Linux, if you need to perform such import and export operations, you 'd better combine them with shell scripts. To avoid writing format parameters every time, you can save the string in the variable, as follows: (File MySQL. SH)

Bash code
  1. #! /Bin/sh
  2. # Copyright (c) 2010 codingstandards. All rights reserved.
  3. # File: mysql. Sh
  4. # Description: operate MySQL Databases in bash
  5. # License: lgpl
  6. # Author: codingstandards
  7. # Email: codingstandards@gmail.com
  8. # Version: 1.0
  9. # Date: 2010.02.28
  10. # Command line parameters in CSV format when importing and exporting data in MySQL
  11. # When exporting data, use: Select... from... [where...] into OUTFILE '/tmp/data.csv' $ mysql_csv_format;
  12. # When importing data, use: load data infile '/tmp/data.csv' into table... $ mysql_csv_format;
  13. # CSV standard documentation: RFC 4180
  14. Mysql_csv_format = "fields terminated by ', 'optionally enclosed by' \" 'scaped by '\ "'Lines terminated by' \ r \ N '"
#! /Bin/sh # copyright (c) 2010 codingstandards. all rights reserved. # file: MySQL. sh # Description: bash in the MySQL database # license: lgpl # Author: codingstandards # Email: codingstandards@gmail.com # version: 1.0 # Date: 2010.02.28 # MySQL import export data, command line parameters when using CSV format # When exporting data, use: select... from... [Where...] into OUTFILE '/tmp/data.csv' $ mysql_csv_format; # Use: load data infile '/tmp/data.csv 'into table... $ mysql_csv_format; # CSV standard document: RFC 4180mysql_csv_format = "fields terminated ', 'optionally enclosed by '\ "'escaped by' \" 'Lines terminated by '\ r \ N '"

 

Example: (File test_mysql_csv.sh)

Bash code
  1. #! /Bin/sh
  2. ./Opt/shtools/commons/MySQL. Sh
  3. # Mysql_csv_format = "fields terminated by ', 'optionally enclosed by' \" 'escaped by '\ "'Lines terminated by' \ r \ N '"
  4. Echo "mysql_csv_format = $ mysql_csv_format"
  5. Rm/tmp/test.csv
  6. Mysql-p -- default-character-set = GBK-t -- verbose test <EOF
  7. Use test;
  8. Create Table if not exists test_info (
  9. Id integer not null,
  10. Content varchar (64) not null,
  11. Primary Key (ID)
  12. );
  13. Delete from test_info;
  14. Insert into test_info values (2010, 'Hello, line
  15. Suped
  16. SEPED
  17. "
  18. End'
  19. );
  20. Select * From test_info;
  21. -- Select * From test_info into OUTFILE '/tmp/test.csv 'fields terminated by', 'optionally enclosed by' "'escaped by'" 'Lines terminated by' \ r \ n ';
  22. Select * From test_info into OUTFILE '/tmp/test.csv' $ mysql_csv_format;
  23. Delete from test_info;
  24. -- Load data infile '/tmp/test.csv' into Table test_info fields terminated by ', 'optionally enclosed by' "'fig by '" 'Lines terminated by' \ r \ n ';
  25. Load data infile '/tmp/test.csv' into Table test_info $ mysql_csv_format;
  26. Select * From test_info;
  27. EOF
  28. Echo "==== content in/tmp/test.csv ===="
  29. CAT/tmp/test.csv
#! /Bin/sh. /opt/shtools/commons/MySQL. sh # mysql_csv_format = "fields terminated ', 'optionally enclosed by '\ "'escaped by' \" 'Lines terminated by '\ r \ N' "Echo" mysql_csv_format = $ mysql_csv_format "RM/tmp/test.csv mysql-p -- default-character-set = GBK-t -- verbose test <eofuse test; create Table if not exists test_info (ID integernot null, content varchar (64) not null, primary key (ID); Delete from test_info; insert into test_info values (2010, 'Hello, linesupedseped "end'); select * From test_info; -- select * From test_info into OUTFILE '/tmp/test.csv 'fields terminated ', 'optionally enclosed by '"'elasticed by'" 'Lines terminated by '\ r \ n'; select * From test_info into outfile'/tmp/test.csv' $ mysql_csv_format; delete from test_info; -- load data infile '/tmp/test.csv 'into table test_info fields terminated ', 'optionally enclosed by '"'elasticed by'" 'Lines terminated by '\ r \ n'; load data infile'/tmp/test.csv 'into Table test_info $ mysql_csv_format; select * From test_info; eofecho "==== content in/tmp/test.csv ===" cat/tmp/test.csv

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.