The sample SQL statements for exporting CSV format data in MySQL are as follows:
SQL code
- SELECT * FROM test_info
- into outfile '/tmp/test.csv '
- Fields terminated by ', ' optionally enclosed by ' "' escaped by '" '
- Lines terminated by ' \ r \ n ';
[SQL]View Plaincopy print?
- SELECT * FROM test_info
- into outfile '/tmp/test.csv '
- Fields terminated by ', ' optionally enclosed by ' "' escaped by '" '
- Lines terminated by ' \ r \ n ';
The sample SQL statements for importing CSV format data in MySQL are as follows:
SQL code
- Load data infile '/tmp/test.csv '
- into table Test_info
- Fields terminated by ', ' optionally enclosed by ' "' escaped by '" ' /c5>
- Lines terminated by ' \ r \ n ';
[SQL]View Plaincopy print?
- Load data infile '/tmp/test.csv '
- into table Test_info
- Fields terminated by ', ' optionally enclosed by ' "' escaped by '" ' /c5>
- Lines terminated by ' \ r \ n ';
Load data infile '/tmp/test.csv ' into table test_info fields terminated by ', ' optionally enclosed by ' "' escape D by ' "' lines terminated by ' \ r \ n ';
The most critical part of this is the format parameter.
SQL code
- Fields terminated by ', ' optionally enclosed by ' "' escaped by '" '
- Lines terminated by ' \ r \ n '
[SQL]View Plaincopy print?
- Fields terminated by ', ' optionally enclosed by ' "' escaped by '" '
- Lines terminated by ' \ r \ n '
Fields terminated by ', ' optionally enclosed by ' "' escaped by '" ' lines terminated by ' \ r \ n '
This parameter is set according to the RFC4180 document, which is the full name common format and MIME Type for comma-separated Values (CSV) Files, which detail the CSV format, and the key points include:
(1) The fields are separated by commas, and the data rows are separated by \ r \ n;
(2) The string is surrounded by a half-width double quotation mark, and the string itself is represented by a double quotation mark in two double quotes.
Files: Test_csv.sql
SQL code
- Use test;
- Create table Test_info (
- ID integer not null,
- Content varchar (+) is not null,
- primary key (ID)
- );
- Delete from test_info;
- Insert INTO test_info values ("Hello, line
- suped
- seped
- "
- End
- );
- SELECT * from test_info;
- SELECT * from test_info to outfile '/tmp/test.csv ' fields terminated by ', ' optionally enclosed By ', ' escaped 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 ' C6>by ' "' escaped by '" ' Lines terminated by ' \ r \ n ';
- SELECT * from test_info;
[SQL]View Plaincopy print?
- Use test;
- Create table Test_info (
- ID integer not null,
- Content varchar (+) is not null,
- primary key (ID)
- );
- Delete from test_info;
- Insert INTO test_info values ("Hello, line
- suped
- seped
- "
- End
- );
- SELECT * from test_info;
- SELECT * from test_info to outfile '/tmp/test.csv ' fields terminated by ', ' optionally enclosed By ', ' escaped 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 ' C6>by ' "' escaped by '" ' Lines terminated by ' \ r \ n ';
- SELECT * from test_info;
Use Test;create table Test_info (ID integernot null,content varchar ($) Not null,primary key (ID));d elete from Test_info;i Nsert into test_info values (' Hello, linesupedseped ' end '); select * FROM Test_info;select * from Test_info to Outfi Le '/tmp/test.csv ' terminated by ', ' optionally enclosed by ' "' escaped by '" ' lines terminated by ' \ r \ n ';d elete fr Om test_info;load data infile '/tmp/test.csv ' into table test_info fields terminated by ', '
Files: test.csv
Text Code
- , "Hello, line
- suped
- seped
- ""
- End
[Text]View Plaincopy print?
- , "Hello, line
- suped
- seped
- ""
- End
"Hello, linesupedseped" "End"
Under Linux If you often want to do such import and export operations, of course, it is best to combine with shell script, in order to avoid each time to write the format parameters, you can save this string in the variable, as follows: (File mysql.sh)
Bash code
- #!/bin/sh
- # Copyright (c) codingstandards. All rights reserved.
- # file:mysql.sh
- # operating MySQL database in Description:bash
- # LICENSE:LGPL
- # Author:codingstandards
- # email: [Email protected]
- # Version: 1.0
- # Date: 2010.02. -
- # When importing and exporting data in MySQL, command-line arguments when using CSV format
- # When exporting data using: Select ... from ... [Where ...] into outfile '/tmp/data.csv ' $MYSQL _csv_format;
- # used when importing data: Load data infile '/tmp/data.csv ' into table ... $MYSQL _csv_format;
- # CSV standard document: RFC 4180
- Mysql_csv_format="Fields terminated by ', ' optionally enclosed by ' \" ' escaped by ' \ "' lines terminated by ' \ r \ n '"
#!/bin/sh# Copyright (c) codingstandards. All rights reserved.# file:mysql.sh# Description:bash operation MySQL Database # license:lgpl# author:codingstandards# email: [Email protected]# version:1.0# date:2010.02.28# When importing and exporting data in MySQL, the command line arguments # when using CSV format are used when exporting data: Select ... [Where ...] into outfile '/tmp/data.csv ' $MYSQL _csv_format;# used when importing data: Load data infile '/tmp/data.csv ' into table ... $MYSQL _csv_form at;# CSV Standard document: RFC 4180mysql_csv_format= "Fields terminated by ', ' optionally enclosed by ' \" ' escaped by ' \ "' Lines Terminat Ed by ' \ r \ n ' "
Examples of use are as follows: (File test_mysql_csv.sh)
Bash code
- #!/bin/sh
- . /opt/shtools/commons/mysql.sh
- # mysql_csv_format="Fields terminated by ', ' 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 <<eof
- Use test;
- CREATE table if not exists test_info (
- ID integer NOT NULL,
- Content varchar (+) is not NULL,
- Primary KEY (ID)
- );
- Delete from Test_info;
- INSERT into test_info values ("Hello, Line
- suped
- seped
- "
- End
- );
- SELECT * from Test_info;
- --select * from Test_info to outfile '/tmp/test.csv ' fields terminated by ', ' optionally enclosed by ' "' ES Caped by ' "' lines terminated by ' \ r \ n ';
- SELECT * from Test_info to outfile '/tmp/test.csv ' $MYSQL _csv_format;
- Delete from Test_info;
- --Load data infile '/tmp/test.csv ' into table test_info fields terminated by ', ' optionally enclosed by ' ' Escaped by ' "' lines terminated by ' \ r \ n ';
- Load data infile '/tmp/test.csv ' into table Test_info $MYSQL _csv_format;
- SELECT * from Test_info;
- Eof
- echo "===== content in/tmp/test.csv ====="
- Cat/tmp/test.csv
Transferred from: http://blog.csdn.net/sara_yhl/article/details/6850107
MySQL command line import and export data