解析csv数据导入mysql的方法
发布时间:2020-12-13 06:29:24 所属栏目:PHP教程 来源:网络整理
导读:mysql自己有个csv引擎,可以通过这个引擎来实现将csv中的数据导入到mysql数据库中,并且速度比通过php或是python写的批处理程序快的多。 具体的实现代码示例: div class="codetitle" a style="CURSOR: pointer" data="54964" class="copybut" id="copybut54
mysql自己有个csv引擎,可以通过这个引擎来实现将csv中的数据导入到mysql数据库中,并且速度比通过php或是python写的批处理程序快的多。 具体的实现代码示例:<div class="codetitle"><a style="CURSOR: pointer" data="54964" class="copybut" id="copybut54964" onclick="doCopy('code54964')"> 代码如下:<div class="codebody" id="code54964">load data infile '/tmp/file.csv' into table _tablename (set character utf8) fields terminated by ',' enclosed by '"' lines terminated by 'rn'; 这段代码中涉及的一些关键字的解释如下:fields terminated by '':这是指出csv文件中字段终止符,也就是数据之间的分隔符;enclosed by '':指出封套符; lines terminated by '':指行终止符 在csv文档(RFC4180)中详细介绍了csv的格式,其中的要点有:(1)字段之间以“,”(逗号)间隔,数据行之间使用rn分隔;(2)字符串以半角双引号包围,字符串本身的双引号用两个双引号表示。 通过以上的解释,详细对于数据导入代码应该有更好的理解了。同样的,csv数据能够导入mysql数据库中,mysql中的数据表也能导出csv文件,导出的代码示例: <div class="codetitle"><a style="CURSOR: pointer" data="46678" class="copybut" id="copybut46678" onclick="doCopy('code46678')"> 代码如下:<div class="codebody" id="code46678"> select * from tablename into outfile '/tmp/data.txt' fields terminated by ',' optionally enclosed by '"' lines terminated by 'n'; 当将数据库中的数据导出到文件后,要再将数据导入到数据库中,必须遵守导出时的文件中定义的格式。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |