解析csv数据导入mysql的方法_php技巧
May 17, 2016 am 08:57 AM
csv
mysql
import
mysql自己有个csv引擎,可以通过这个引擎来实现将csv中的数据导入到mysql数据库中,并且速度比通过php或是python写的批处理程序快的多。
具体的实现代码示例:
复制代码 代码如下:
load data infile '/tmp/file.csv' into table _tablename (set character utf8)
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n';
这段代码中涉及的一些关键字的解释如下:
fields terminated by '':这是指出csv文件中字段终止符,也就是数据之间的分隔符;
enclosed by '':指出封套符;
lines terminated by '':指行终止符
在csv文档(RFC4180)中详细介绍了csv的格式,其中的要点有:
(1)字段之间以“,”(逗号)间隔,数据行之间使用 \n分隔;
(2)字符串以半角双引号包围,字符串本身的双引号用两个双引号表示。
通过以上的解释,详细对于数据导入代码应该有更好的理解了。
同样的,csv数据能够导入mysql数据库中,mysql中的数据表也能导出csv文件,导出的代码示例:
复制代码 代码如下:
select * from tablename into outfile '/tmp/data.txt'
fields terminated by ','
optionally enclosed by '"'
lines terminated by '\n';
当将数据库中的数据导出到文件后,要再将数据导入到数据库中,必须遵守导出时的文件中定义的格式。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP?

How to create a MySQL table using PHP?
