Home  >  Article  >  Backend Development  >  读取txt文件数据存入数据库的问题

读取txt文件数据存入数据库的问题

WBOY
WBOYOriginal
2016-06-23 13:45:501140browse

我每天会下载一个txt的记录文件(过去7天的记录)导入到数据库,txt文件里面大概有2000多条记录,其中80%都是数据库已存在的,只20%左右的数据是新数据,我现在是直接循环读取txt文件写入到数据库,重复的就会失败,但感觉这样效率太低了,请问有没有比较高效一点的方法?


回复讨论(解决方案)

txt中新数据是固定添加在文件尾,还是随机穿插在老数据中?

已知数据是过去7天的,那么可从
1、数据库中读取开始日期之后的数据到数组
2、读取数据文件到数组
3、对两数组求一个差集
4、将差集插入数据库

把这个导进去一个新表...
然后 写sql 批量insert into  ,
条件的时候写where not in (select  主键那些  form  txt文件导入的新表)
希望能帮到你!

1   从数据库中读取文件到一个txt文件old.txt

2   读取old.txt文件行数

3   从new.txt文件指定行数开始读取

仅供参考.

Statement:
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