csv批量匯入mysql命令

2021-07-03 19:51:57 字數 1511 閱讀 4553

基本語法:

load data [low_priority] [local] infile 'file_name txt' [replace | ignore]

into

table tbl_name

[character

set gbk]

[fields

[terminated by

't']

[optionally] enclosed by

''][escaped by

'\' ]]

[lines terminated by'n']

[ignore number lines]

[(col_name, )]

匯入命令_example:

load data infile 'csv檔案路徑\\test.csv'

replace

into

table 表名

fields terminated by

','

optionally enclosed by

'"'

lines terminated by

'\n'

ignore 1 lines(id,@dummy,dayofweek,pddistrict,address,x,y);

不要小看這個簡單的example,包含了很多東西;

第一行就是匯入檔案;

第三~四行很簡單就是每個具體字段內容之間是以逗號隔開的,那就以逗號分開。

erminated by描述欄位的分隔符,預設情況下是tab字元(\t)

enclosed by描述的是字段的括起字元,就是說欄位中如果有引號,就當做是字段的一部分。

語法中還有乙個是 escaped by, 它描述的是轉義字元。預設的是反斜槓(backslash:\ )

第五行 lines terminated by是對每行進行分割,這裡要注意乙個問題,如果csv檔案是在windows下生成,那分割用 『\r\n』,linux下用 『\n』。

第六行中 ignore 1 lines 是忽略第一行,因為第一行往往是欄位名,後邊括號中有個字段很特別 @dummy,它是說如果csv檔案中有個字段我不想插進去,那就把對應欄位名變成@dummy。

ps:想順便插入匯入時間,就在最後加上set update_time=current_timestamp;

同時附帶上匯出命令:

select * from 表名

into outfile '匯出路徑\\test.csv'

fields terminated by

','

optionally enclosed by

'"'

escaped by

'"'

lines terminated by

'\n';

希望多多交流!

csv批量匯入mysql

使用python將csv檔案批量匯入mysql 附上 coding utf 8 換成讀入pandas再讀進sql的方法 import pandas as pd import numpy as np import csv from pandas import series,dataframe 將csv...

Mysql 匯入csv檔案

mysql load data infile命令可以把csv平面檔案中的資料匯入到資料庫中。linux下 load data infile home test dump ip location.csv into table ip location character set utf8 fields ...

CSV檔案匯入MySQL

1 首先看一下我本次匯入的資料,比較簡單 1 在資料庫中首先建立了乙個名為 test 的資料庫,在test資料庫下建立了乙個名為 student 的 屬性如下 column name datatype note idint 11 primary key,not null name varchar 4...