標籤:mysql csv
在工作中經常遇到需要往MySQL資料庫中匯入開發人員提供的資料,其中一種是csv格式的,匯入方式如下:
建立相應的表格
mysql> use kevin;Database changedmysql> CREATE TABLE GeoPC_Places ( -> ISO varchar(2) NOT NULL, -> Country varchar(50) NOT NULL, -> Language varchar(2) NOT NULL, -> ID bigint(20) NOT NULL, -> Region1 varchar(80), -> Region2 varchar(80), -> Region3 varchar(80), -> Region4 varchar(80), -> Locality varchar(80), -> Postcode varchar(15), -> Suburb varchar(80), -> Latitude double, -> Longitude double, -> Elevation integer, -> ISO2 varchar(10), -> FIPS varchar(10), -> NUTS varchar(12), -> HASC varchar(12), -> STAT varchar(20), -> Timezone varchar(30), -> UTC varchar(10), -> DST varchar(10), -> PRIMARY KEY (Language, ID) -> ) ENGINE=Innodb DEFAULT CHARSET=utf8;Query OK, 0 rows affected (0.08 sec)
通過load data命令匯入csv資料,load data文法如下:
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE ‘file_name.csv‘ [REPLACE | IGNORE] INTO TABLE tbl_name [FIELDS [TERMINATED BY ‘string‘] [[OPTIONALLY] ENCLOSED BY ‘char‘] [ESCAPED BY ‘char‘ ] ] [LINES [STARTING BY ‘string‘] [TERMINATED BY ‘string‘] ] [IGNORE number LINES] [(col_name_or_user_var,...)] [SET col_name = expr,...)]
匯入資料:
mysql> load data local infile ‘/dump/GeoPC_AU_Places.csv‘ into table GeoPC_Places -> fields terminated by ‘;‘ -> lines terminated by ‘\n‘ -> ignore 1 lines;Query OK, 15828 rows affected (1.18 sec)Records: 15828 Deleted: 0 Skipped: 0 Warnings: 0mysql> select count(*) from GeoPC_Places;+----------+| count(*) |+----------+| 15828 |+----------+1 row in set (0.10 sec)
匯入成功
相對應的有匯入,肯定有匯出,這裡不再詳細說明,附上select匯出檔案文法共參閱:
SELECT文法
SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr, ... [INTO OUTFILE ‘file_name‘ export_options | INTO DUMPFILE ‘file_name‘] [FROM table_references [WHERE where_definition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_definition] [ORDER BY {col_name | expr | position} [ASC | DESC] , ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [FOR UPDATE | LOCK IN SHARE MODE]]
本文出自 “DBA的天空” 部落格,請務必保留此出處http://kevinora.blog.51cto.com/9406404/1675790
MySQL匯入csv資料