Export/Import CSV files with MySQL_MySQL

WBOY
풀어 주다: 2016-06-01 13:51:23
원래의
994명이 탐색했습니다.

Export/Import CSV files with MySQL – No external tool required

Loading data from and to CSV or other TAB DELIMITED or similar file format is essential in day to day operation. With MySQL it is easy to load data into table using files and exporting data into CSV files is quite easy. No need to use any external tool it can be done right there from the query tool/command prompt, no specific GUI tool required.

Let's see how?

Exporting data as CSV file directly using simple MySQL Query

Simply add INTO OUTFILE [FILE_PATH] in your query and you are done. Here is a simple example:

SELECT * FROM [TABLE]<br />INTO OUTFILE 'X:/[FILE]'
로그인 후 복사

Above statement uses default options for file export of result set of a query, but it is simple to override them. By default file will be TAB DELIMITED file. We can always override as showed in example below:

SELECT * FROM  [TABLE] INTO OUTFILE 'X:/[FILE]'<br />FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'<br />LINES TERMINATED BY 'n'
로그인 후 복사

OR

SELECT * INTO OUTFILE 'X:/[FILE]'<br />FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'<br />LINES TERMINATED BY 'n'<br />FROM  [TABLE]
로그인 후 복사

The beauty is it don't really matter where you place FROM TABLE clause, any of the style would work from above example. Don't forget that we can use all our complex join or where conditions which we really use with SELECT.

Full Syntex:

SELECT<br />[ALL | DISTINCT | DISTINCTROW ]<br />[HIGH_PRIORITY]<br />[STRAIGHT_JOIN]<br />[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]<br />[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]<br />select_expr [, select_expr ...]<br />[FROM table_references<br />[WHERE where_condition]<br />[GROUP BY {col_name | expr | position}<br />[ASC | DESC], ... [WITH ROLLUP]]<br />[HAVING where_condition]<br />[ORDER BY {col_name | expr | position}<br />[ASC | DESC], ...]<br />[LIMIT {[offset,] row_count | row_count OFFSET offset}]<br />[PROCEDURE procedure_name(argument_list)]<br />[INTO OUTFILE 'file_name' export_options<br />| INTO DUMPFILE 'file_name'<br />| INTO var_name [, var_name]]<br />[FOR UPDATE | LOCK IN SHARE MODE]]
로그인 후 복사

Importing data from CSV file directly using simple MySQL Query

Here is the simplest way to do it:

LOAD DATA INFILE 'X:/[FILE]'<br />INTO TABLE [TABLE]
로그인 후 복사

Similar to Export option of MySQL it will expect file to be TAB DELIMITED if we don't specify any option for it in it's simplest form. So if you simple EXPORT file without any custom specification as a first query example it would simple LOAD DATA from that file, but if you have a file with custom separator than it needs to be specified.

LOAD DATA INFILE 'X:/[FILE]'<br />INTO TABLE [TABLE]
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY 'n'
로그인 후 복사

Full syntex:

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'<br />[REPLACE | IGNORE]<br />INTO TABLE tbl_name<br />[CHARACTER SET charset_name]<br />[{FIELDS | COLUMNS}<br />[TERMINATED BY 'string']<br />[[OPTIONALLY] ENCLOSED BY 'char']<br />[ESCAPED BY 'char']<br />]<br />[LINES<br />[STARTING BY 'string']<br />[TERMINATED BY 'string']<br />]<br />[IGNORE number LINES]<br />[(col_name_or_user_var,...)]<br />[SET col_name = expr,...]
로그인 후 복사

So exporting and importing data from MySQL seems quite easy and we do not need to write any programming statement to format them just a simple query can do it.

References: (For more reference please refer)
http://dev.mysql.com/doc/refman/5.0/en/select.html
http://dev.mysql.com/doc/refman/5.1/en/load-data.html

Similar Posts:

  • HeidiSQL 5.1 – good has got better
  • HeidiSQL 4.0 RC1 released
  • Using Views to avoid cross database connection – MySQL
  • Alternate to FIND_IN_SET for non-MySQL databases
  • MySQL 5.5.8 – Problem while Creating new User (SQL Error (1364): Field ‘authentication_string’ doesn’t have a default value)
?
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!