Bash中使用MySQL匯入匯出CSV格式資料

2021-08-30 12:11:40 字數 3580 閱讀 7716

mysql中匯出csv格式資料的sql語句樣本如下:

select * from test_info 

into outfile '/tmp/test.csv'

fields terminated by ',' optionally enclosed by '"' escaped by '"'

lines terminated by '\r\n';

mysql中匯入csv格式資料的sql語句樣本如下:

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)字串以半形雙引號包圍,字串本身的雙引號用兩個雙引號表示。

檔案:test_csv.sql

use test;

create table 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';

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';

select * from test_info;

檔案:test.csv

2010,"hello, line

suped

seped

""end"

在linux下如果經常要進行這樣的匯入匯出操作,當然最好與shell指令碼結合起來,為了避免每次都要寫格式引數,可以把這個串儲存在變數中,如下所示:(檔案mysql.sh)

#!/bin/sh

# file: mysql.sh

# description: bash中操作mysql資料庫

# license: lgpl

# author: codingstandards

# email: [email protected]

# version: 1.0

# date: 2010.02.28

# mysql中匯入匯出資料時,使用csv格式時的命令列引數

# 在匯出資料時使用:select ... from ... [where ...] into outfile '/tmp/data.csv' $mysql_csv_format;

# 在匯入資料時使用:load data infile '/tmp/data.csv' into table ... $mysql_csv_format;

# csv標準文件:rfc 4180

mysql_csv_format="fields terminated by ',' optionally enclosed by '\"' escaped by '\"' lines terminated by '\r\n'"

使用示例如下:(檔案test_mysql_csv.sh)

#!/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 if not 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;

eofecho "***** content in /tmp/test.csv *****"

cat /tmp/test.csv

使用SparkSql從Mysql中匯入匯出資料

匯入資料 建立properties物件,配置mysql的密碼和使用者名稱 val properties new properties properties.setproperty user username properties.setproperty password password 呼叫spa...

mysql的匯入匯出命令 mysql匯入匯出命令

一 匯出資料庫用mysqldump命令 注意mysql的安裝路徑,即此命令的路徑 1 匯出資料和表結構 mysqldump u使用者名稱 p密碼 資料庫名 資料庫名.sql usr local mysql bin mysqldump uroot p abc abc.sql 敲回車後會提示輸入密碼 2...

thinkphp中使用PHPEXCEL匯入資料

匯入方法比較簡單 但必須考慮到excel本身單元格格式問題 例如以0開頭的字串讀出來被去掉了前導0 成為float型而丟失一位 必須進行處理 author lizhaoyao add time 2015 08 26 php excel import 使用者匯入excel param type fil...