MySQL資料庫如何用命令列匯出帶表頭和不帶表頭的csv檔案
實驗如下:
建表:
mysql> CREATE TABLE `test` (
-> `id` varchar(64) NOT NULL,
-> `ecode` varchar(10) DEFAULT NULL,
-> `type` varchar(12) DEFAULT NULL,
-> `timeid` varchar(12) DEFAULT NULL,
-> `start_time` date DEFAULT NULL,
-> `end_time` varchar(12) DEFAULT NULL,
-> PRIMARY KEY (`id`),
-> KEY `start` (`start_time`),
-> KEY `end` (`end_time`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.35 sec)
mysql>
mysql>
插入資料:
mysql> insert into test select * from date_rule;
Query OK, 1412 rows affected (0.49 sec)
Records: 1412 Duplicates: 0 Warnings: 0
不帶表頭:
mysql> select * from test into outfile 'd:\test.csv' fields terminated by ','enclosed by '"'lines terminated by '\r\n';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
匯出報錯,原因是5.7版本對mysqld 的匯入匯出做限制,解決辦法:
在my.ini中加上
[mysqld]
secure_file_priv=''
重啟資料庫使配置生效
PS C:\WINDOWS\system32> net stop mysql
MySQL 服務正在停止.
MySQL 服務已成功停止。
PS C:\WINDOWS\system32> net start mysql
MySQL 服務正在啟動 ..
MySQL 服務已經啟動成功。
再次執行命令成功:
mysql> select * from test into outfile "d:/test.csv" fields terminated by ','enclosed by '"'lines terminated by '\r\n';
Query OK, 1412 rows affected (0.00 sec)
用Notepad++開啟檔案發現沒表頭:
"00000c-month-20161","00000c","month","20161","2015-12-29","2016-01-28"
"00000c-month-201610","00000c","month","201610","2016-09-29","2016-10-28"
"00000c-month-201611","00000c","month","201611","2016-10-29","2016-11-28"
"00000c-month-201612","00000c","month","201612","2016-11-29","2016-12-28"
"00000c-month-20162","00000c","month","20162","2016-01-29","2016-02-28"
"00000c-month-20163","00000c","month","20163","2016-02-29","2016-03-28"
"00000c-month-20164","00000c","month","20164","2016-03-29","2016-04-28"
"00000c-month-20165","00000c","month","20165","2016-04-29","2016-05-28"
"00000c-month-20166","00000c","month","20166","2016-05-29","2016-06-28"
"00000c-month-20167","00000c","month","20167","2016-06-29","2016-07-28"
"00000c-month-20168","00000c","month","20168","2016-07-29","2016-08-28"
"00000c-month-20169","00000c","month","20169","2016-08-29","2016-09-28"
"00000c-month-20171","00000c","month","20171","2016-12-29","2017-01-28"
"00000c-month-201710","00000c","month","201710","2017-09-29","2017-10-28"
"00000c-month-201711","00000c","month","201711","2017-10-29","2017-11-28"
.........................................................................
.........................................................................
檢視錶結構:
mysql> desc test;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | varchar(64) | NO | PRI | NULL | |
| ecode | varchar(10) | YES | | NULL | |
| type | varchar(12) | YES | | NULL | |
| timeid | varchar(12) | YES | | NULL | |
| start_time | date | YES | MUL | NULL | |
| end_time | varchar(12) | YES | MUL | NULL | |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
帶表頭匯出csv:
mysql> select * from (select 'id','ecode','type','timeid','start_time','end_time' union all select id,ecode,type,timeid,start_time,end_time from test) b into outfile "d:/test.csv" fields terminated by ','enclosed by '"'lines terminated by '\r\n';
Query OK, 1413 rows affected (0.01 sec)
用Notepad++開啟檔案發現帶表頭:
"id","ecode","type","timeid","start_time","end_time"
"00000c-month-20161","00000c","month","20161","2015-12-29","2016-01-28"
"00000c-month-201610","00000c","month","201610","2016-09-29","2016-10-28"
"00000c-month-201611","00000c","month","201611","2016-10-29","2016-11-28"
"00000c-month-201612","00000c","month","201612","2016-11-29","2016-12-28"
"00000c-month-20162","00000c","month","20162","2016-01-29","2016-02-28"
"00000c-month-20163","00000c","month","20163","2016-02-29","2016-03-28"
"00000c-month-20164","00000c","month","20164","2016-03-29","2016-04-28"
.................................................................................
建表:
mysql> CREATE TABLE `test` (
-> `id` varchar(64) NOT NULL,
-> `ecode` varchar(10) DEFAULT NULL,
-> `type` varchar(12) DEFAULT NULL,
-> `timeid` varchar(12) DEFAULT NULL,
-> `start_time` date DEFAULT NULL,
-> `end_time` varchar(12) DEFAULT NULL,
-> PRIMARY KEY (`id`),
-> KEY `start` (`start_time`),
-> KEY `end` (`end_time`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.35 sec)
mysql>
mysql>
插入資料:
mysql> insert into test select * from date_rule;
Query OK, 1412 rows affected (0.49 sec)
Records: 1412 Duplicates: 0 Warnings: 0
不帶表頭:
mysql> select * from test into outfile 'd:\test.csv' fields terminated by ','enclosed by '"'lines terminated by '\r\n';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
匯出報錯,原因是5.7版本對mysqld 的匯入匯出做限制,解決辦法:
在my.ini中加上
[mysqld]
secure_file_priv=''
重啟資料庫使配置生效
PS C:\WINDOWS\system32> net stop mysql
MySQL 服務正在停止.
MySQL 服務已成功停止。
PS C:\WINDOWS\system32> net start mysql
MySQL 服務正在啟動 ..
MySQL 服務已經啟動成功。
再次執行命令成功:
mysql> select * from test into outfile "d:/test.csv" fields terminated by ','enclosed by '"'lines terminated by '\r\n';
Query OK, 1412 rows affected (0.00 sec)
用Notepad++開啟檔案發現沒表頭:
"00000c-month-20161","00000c","month","20161","2015-12-29","2016-01-28"
"00000c-month-201610","00000c","month","201610","2016-09-29","2016-10-28"
"00000c-month-201611","00000c","month","201611","2016-10-29","2016-11-28"
"00000c-month-201612","00000c","month","201612","2016-11-29","2016-12-28"
"00000c-month-20162","00000c","month","20162","2016-01-29","2016-02-28"
"00000c-month-20163","00000c","month","20163","2016-02-29","2016-03-28"
"00000c-month-20164","00000c","month","20164","2016-03-29","2016-04-28"
"00000c-month-20165","00000c","month","20165","2016-04-29","2016-05-28"
"00000c-month-20166","00000c","month","20166","2016-05-29","2016-06-28"
"00000c-month-20167","00000c","month","20167","2016-06-29","2016-07-28"
"00000c-month-20168","00000c","month","20168","2016-07-29","2016-08-28"
"00000c-month-20169","00000c","month","20169","2016-08-29","2016-09-28"
"00000c-month-20171","00000c","month","20171","2016-12-29","2017-01-28"
"00000c-month-201710","00000c","month","201710","2017-09-29","2017-10-28"
"00000c-month-201711","00000c","month","201711","2017-10-29","2017-11-28"
.........................................................................
.........................................................................
檢視錶結構:
mysql> desc test;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | varchar(64) | NO | PRI | NULL | |
| ecode | varchar(10) | YES | | NULL | |
| type | varchar(12) | YES | | NULL | |
| timeid | varchar(12) | YES | | NULL | |
| start_time | date | YES | MUL | NULL | |
| end_time | varchar(12) | YES | MUL | NULL | |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
帶表頭匯出csv:
mysql> select * from (select 'id','ecode','type','timeid','start_time','end_time' union all select id,ecode,type,timeid,start_time,end_time from test) b into outfile "d:/test.csv" fields terminated by ','enclosed by '"'lines terminated by '\r\n';
Query OK, 1413 rows affected (0.01 sec)
用Notepad++開啟檔案發現帶表頭:
"id","ecode","type","timeid","start_time","end_time"
"00000c-month-20161","00000c","month","20161","2015-12-29","2016-01-28"
"00000c-month-201610","00000c","month","201610","2016-09-29","2016-10-28"
"00000c-month-201611","00000c","month","201611","2016-10-29","2016-11-28"
"00000c-month-201612","00000c","month","201612","2016-11-29","2016-12-28"
"00000c-month-20162","00000c","month","20162","2016-01-29","2016-02-28"
"00000c-month-20163","00000c","month","20163","2016-02-29","2016-03-28"
"00000c-month-20164","00000c","month","20164","2016-03-29","2016-04-28"
.................................................................................
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2146104/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- linux 下MySQL命令列匯出csv檔案格式LinuxMySql命令列
- javascript: 帶分組資料的Table表頭排序JavaScript排序
- mysql中不輸出表頭和結尾的方法MySql
- PHP 匯出大資料 CSV 檔案PHP大資料
- C++標準庫名字和標頭檔案--表C++
- Mysql匯出表結構、表資料MySql
- tsdump-用於匯出資料庫表結構的工具(支援匯出為text、markdown、csv、json)資料庫JSON
- 如何把 .csv 的檔案匯入資料庫SQL SERVER 中!資料庫SQLServer
- mysql匯入匯出.csv格式資料MySql
- MySQL表資料匯入與匯出MySql
- Java解析ELF檔案:使用Java讀取檔案頭部、節區頭部表、程式頭部表Java
- 如何將 JSON, Text, XML, CSV 資料檔案匯入 MySQLJSONXMLMySql
- mongodb使用自帶命令工具匯出匯入資料MongoDB
- MySQL匯出資料為csv的方法MySql
- Mysql匯入csv檔案MySql
- DB2資料庫匯出表結構與匯入、匯出表資料DB2資料庫
- Excel如何列印固定表頭和表尾Excel
- MYSQL 匯出資料庫中某張表的部分數…MySql資料庫
- 抽取exp/expdp匯出檔案頭的資訊
- [MySQL Help]MySQL Workbench用csv格式匯出資料亂碼MySql
- 關於資料表結構sql檔案匯入mysql資料庫的問題?MySql資料庫
- java匯出CSV檔案Java
- 把csv檔案的資料匯入到oracle資料庫中Oracle資料庫
- MySQL 匯出資料為csv格式的方法MySql
- mysql匯出某個表的部分資料MySql
- PHP匯出大量資料,儲存為CSV檔案PHP
- 如何用exp 匯出 某個表的某幾列資料
- mysql匯入csv格式檔案MySql
- Oracle資料庫的匯入和匯出命令Oracle資料庫
- Mysql匯出表結構及表資料 mysqldump用法MySql
- 【mysql】資料庫匯出和匯入MySql資料庫
- locate標頭檔案和庫檔案
- 報表如何批次匯出成 excel 檔案Excel
- 檢測根目錄帶bom頭資訊的檔案
- 前端匯出Excel之動態多級表頭前端Excel
- 使用PHP原生匯出Excel和CSV檔案PHPExcel
- ClickHouse 資料表匯出和匯入(qbit)
- PHP 匯出 CSV 格式檔案PHP