MySQL中匯入 匯出CSV

2021-09-06 19:57:22 字數 2395 閱讀 7795

來自:

匯出

select

*from

test_info

into outfile '

/tmp/test.csv

'fields terminated by'

,' optionally enclosed by'"

' escaped by'"

'lines terminated by'

\r\n

';

匯入

load data infile '

/tmp/test.csv

'into

table

test_info

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

'

這個引數是根據rfc4180文件設定的,該文件全稱common format and mime type for comma-separated values (csv) files,其中詳細描述了csv格式,其要點包括:

(1)字段之間以逗號分隔,資料行之間以\r\n分隔;

(2)字串以半形雙引號包圍,字串本身的雙引號用兩個雙引號表示。

shell 例子

#!/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

create table

ifnot exists test_info (

id integer not null

, content varchar(

64) not null

, primary key (

id)

);

delete from test_info;

insert into test_info values (

2010, '

hello, line

suped

seped

" end'

);

select *from test_info;

-- select * from test_info into outfile '

/tmp/test.csv

' fields terminated by '

,' optionally enclosed by '

"' escaped 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 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

P S 向MySQL中匯入CSV檔案

load data infile g adawn com.facebook.katana2.csv ignore 注意,這裡的ignore是關鍵,不匯入重複的行 fields terminated by enclosed by lines terminated by r n csv檔案row之間以 ...

MySQL匯入匯出CSV檔案

mysql自己提供了匯入匯出資料庫的工具,但有時我們需要僅僅匯入匯出單個表的資料,比如匯入匯出csv檔案,此時可以使用mysql自動的命令來做匯入匯出工作。匯出語法如下 select from table into outfile file 或者select from table into outf...

MySQL匯入匯出CSV檔案

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!mysql自己提供了匯入匯出資料庫的工具,但有時我們需要僅僅匯入匯出單個表的資料,比如匯入匯出csv檔案,此時可以使用mysql自動的命令來做匯入匯出工作。匯出語法如下 select from table into outfile file 或者s...