MySQL command line import and export data

Source: Internet
Author: User
Tags rfc mysql command line

The sample SQL statements for exporting CSV format data in MySQL are as follows:

SQL code
    1. SELECT * FROM test_info
    2. into outfile '/tmp/test.csv '
    3. Fields terminated by ', ' optionally enclosed by ' "' escaped by '" '
    4. Lines terminated by ' \ r \ n ';
[SQL]View Plaincopy print?
    1. SELECT * FROM test_info
    2. into outfile '/tmp/test.csv '
    3. Fields terminated by ', ' optionally enclosed by ' "' escaped by '" '
    4. Lines terminated by ' \ r \ n ';

The sample SQL statements for importing CSV format data in MySQL are as follows:

SQL code
    1. Load data infile '/tmp/test.csv '
    2. into table Test_info
    3. Fields terminated by ', ' optionally enclosed by ' "' escaped by '" ' /c5>
    4. Lines terminated by ' \ r \ n ';
[SQL]View Plaincopy print?
    1. Load data infile '/tmp/test.csv '
    2. into table Test_info
    3. Fields terminated by ', ' optionally enclosed by ' "' escaped by '" ' /c5>
    4. 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
    1. Fields terminated by ', ' optionally enclosed by ' "' escaped by '" '
    2. Lines terminated by ' \ r \ n '
[SQL]View Plaincopy print?
    1. Fields terminated by ', ' optionally enclosed by ' "' escaped by '" '
    2. 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
  1. Use test;
  2. Create table Test_info (
  3. ID integer not null,
  4. Content varchar (+) is not null,
  5. primary key (ID)
  6. );
  7. Delete from test_info;
  8. Insert INTO test_info values ("Hello, line
  9. suped
  10. seped
  11. "
  12. End
  13. );
  14. SELECT * from test_info;
  15. SELECT * from test_info to 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 '  C6>by ' "' escaped by '" ' Lines terminated by ' \ r \ n ';
  18. SELECT * from test_info;
[SQL]View Plaincopy print?
  1. Use test;
  2. Create table Test_info (
  3. ID integer not null,
  4. Content varchar (+) is not null,
  5. primary key (ID)
  6. );
  7. Delete from test_info;
  8. Insert INTO test_info values ("Hello, line
  9. suped
  10. seped
  11. "
  12. End
  13. );
  14. SELECT * from test_info;
  15. SELECT * from test_info to 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 '  C6>by ' "' escaped by '" ' Lines terminated by ' \ r \ n ';
  18. 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
    1. , "Hello, line
    2. suped
    3. seped
    4. ""
    5. End
[Text]View Plaincopy print?
    1. , "Hello, line
    2. suped
    3. seped
    4. ""
    5. 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
  1. #!/bin/sh
  2. # Copyright (c) codingstandards.  All rights reserved.
  3. # file:mysql.sh
  4. # operating MySQL database in Description:bash
  5. # LICENSE:LGPL
  6. # Author:codingstandards
  7. # email: [Email protected]
  8. # Version: 1.0
  9. # Date: 2010.02. -
  10. # When importing and exporting data in MySQL, command-line arguments when using CSV format
  11. # When exporting data using: Select ... from ... [Where ...]  into outfile '/tmp/data.csv ' $MYSQL _csv_format;
  12. # used when importing data: Load data infile '/tmp/data.csv ' into table ... $MYSQL _csv_format;
  13. # CSV standard document: RFC 4180
  14. 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
  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 (+) is not NULL,
  11. Primary KEY (ID)
  12. );
  13. Delete from Test_info;
  14. INSERT into test_info values ("Hello, Line
  15. suped
  16. seped
  17. "
  18. End
  19. );
  20. SELECT * from Test_info;
  21. --select * from Test_info to outfile '/tmp/test.csv ' fields terminated by ', ' optionally enclosed by ' "' ES  Caped by ' "' lines terminated by ' \ r \ n ';
  22. SELECT * from Test_info to 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 ' '  Escaped 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

Transferred from: http://blog.csdn.net/sara_yhl/article/details/6850107

MySQL command line import and export data

Related Article

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.