MySQL Export import data-csv

Source: Internet
Author: User
Tags mysql client

MySQL Database export CSV file command:

Mysql> Select First_name,last_name,email from account to outfile ' e://output1.csv ' fields terminated by ', ' Optionall Y enclosed by "lines terminated by '/n ';

CSV file effect:

Sunny Grigoryan [Email protected]
Jon Siegal [Email protected]
Joe Siegal [Email protected]
Alejandro Medina [Email protected]

CVS file import mysql database command:
mysql> Load Data local infile ' e://input1.csv ' into table test1 fields Termin
Ated by ', ' lines terminated by '/n ' (first_name,last_name,email);
Query OK, 1 row affected, 1 Warning (0.00 sec)
records:69 deleted:0 skipped:68 warnings:0

Mysql> select * from Test1;
+----+------------+-----------+--------------------------+
| ID | first_name | last_name | email |
+----+------------+-----------+--------------------------+
| 0 | Sunny | Grigoryan | [Email protected]
+----+------------+-----------+--------------------------+

Fields TERMINATED by----field terminating character
Optionally enclosed by----envelope character
LINES TERMINATED by----line Terminator

Connect to the server through the MySQL client shell, select the database to use, and enter the SQL code:

SELECT * FROM Test_info
into outfile '/tmp/test.csv '
Fields terminated by ', ' optionally-enclosed by ' "' escaped by '" '
Lines terminated by ' \ r \ n ';

The most critical part of this is the format parameter.

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.

By executing the code above, you can export the required data in CSV format to the executed file.

In addition, the SQL code for importing CSV format data in MySQL is as follows:

Load data infile '/tmp/test.csv '
into table Test_info
Fields terminated by ', ' optionally-enclosed by ' "' escaped by '" '
Lines terminated by ' \ r \ n ';

The following statements are useful when the number of columns in a CSV file is less than the database table:

Load data infile "/tmp/appleappv2_search_day_20110525.csv" into table Apple_search_log fields terminated by '; ' Lines term Inated by '/n ' (Apptypeid,keyword,searchtime,model);

The blue Word part is the highlight. This statement is useful if the data table contains self-increment fields, such as the self-increment ID.

MySQL Export import data-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.