天天看点

php读取txt文件并将数据插入到数据库

今天测试一个功能,需要往数据库中插入一些原始数据,PM给了一个txt文件,如何快速的将这个txt文件的内容拆分为所要的数组,然后再插入到数据库中?

serial_number.txt的示例内容:

serial_number.txt:

?

1 2 3 4 5 6

DM00001A11 0116,

SN00002A11 0116,

AB00003A11 0116,

PV00004A11 0116,

OC00005A11 0116,

IX00006A11 0116,

创建数据表:

?

1 2 3 4

create

table

serial_number(

id

int

primary

key

auto_increment

not

null

,

serial_number

varchar

(50)

not

null

)ENGINE=InnoDB

DEFAULT

CHARSET=utf8;

php代码如下:

?

1 2 3 4 5 6 7 8 9 10 11 12 13

$conn

= mysql_connect(

'127.0.0.1'

,

'root'

,

''

)

or

die

(

"Invalid query: "

. mysql_error());

mysql_select_db(

'test'

,

$conn

)

or

die

(

"Invalid query: "

. mysql_error());

$content

=

file_get_contents

(

"serial_number.txt"

);

$contents

=

explode

(

","

,

$content

);

//explode()函数以","为标识符进行拆分

foreach

(

$contents

as

$k

=>

$v

)

//遍历循环

{

$id

=

$k

;

$serial_number

=

$v

;

mysql_query("insert into serial_number (`id`,`serial_number`)

VALUES(

'$id'

,

'$serial_number'

)");

}

备注:方法有很多种,我这里是在拆分txt文件为数组后,然后遍历循环得到的数组,每循环一次,往数据库中插入一次。

再给大家分享一个支持大文件导入的

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

<?php

function

loadTxtDataIntoDatabase(

$splitChar

,

$file

,

$table

,

$conn

,

$fields

=

array

(),

$insertType

=

'INSERT'

){

if

(

empty

(

$fields

))

$head

=

"{$insertType} INTO `{$table}` VALUES('"

;

else

$head

=

"{$insertType} INTO `{$table}`(`"

.implode(

'`,`'

,

$fields

).

"`) VALUES('"

//数据头

$end

=

"')"

;

$sqldata

= trim(

file_get_contents

(

$file

));

if

(preg_replace(

'/\s*/i'

,

''

,

$splitChar

) ==

''

) {

$splitChar

=

'/(\w+)(\s+)/i'

;

$replace

=

"$1','"

;

$specialFunc

=

'preg_replace'

;

}

else

{

$splitChar

=

$splitChar

;

$replace

=

"','"

;

$specialFunc

=

'str_replace'

;

}

//处理数据体,二者顺序不可换,否则空格或Tab分隔符时出错

$sqldata

= preg_replace(

'/(\s*)(\n+)(\s*)/i'

,

'\'),(\''

,

$sqldata

); 

//替换换行

$sqldata

=

$specialFunc

(

$splitChar

,

$replace

,

$sqldata

);       

//替换分隔符

$query

=

$head

.

$sqldata

.

$end

//数据拼接

if

(mysql_query(

$query

,

$conn

))

return

array

(true);

else

{

return

array

(false,mysql_error(

$conn

),mysql_errno(

$conn

));

}

}

//调用示例1

require

'db.php'

;

$splitChar

=

'|'

//竖线

$file

=

'sqldata1.txt'

;

$fields

=

array

(

'id'

,

'parentid'

,

'name'

);

$table

=

'cengji'

;

$result

= loadTxtDataIntoDatabase(

$splitChar

,

$file

,

$table

,

$conn

,

$fields

);

if

(

array_shift

(

$result

)){

echo

'Success!<br/>'

;

}

else

{

echo

'Failed!--Error:'

.

array_shift

(

$result

).

'<br/>'

;

}

//调用示例2

require

'db.php'

;

$splitChar

=

' '

//空格

$file

=

'sqldata2.txt'

;

$fields

=

array

(

'id'

,

'make'

,

'model'

,

'year'

);

$table

=

'cars'

;

$result

= loadTxtDataIntoDatabase(

$splitChar

,

$file

,

$table

,

$conn

,

$fields

);

if

(

array_shift

(

$result

)){

echo

'Success!<br/>'

;

}

else

{

echo

'Failed!--Error:'

.

array_shift

(

$result

).

'<br/>'

;

}

//调用示例3

require

'db.php'

;

$splitChar

=

'  '

//Tab

$file

=

'sqldata3.txt'

;

$fields

=

array

(

'id'

,

'make'

,

'model'

,

'year'

);

$table

=

'cars'

;

$insertType

=

'REPLACE'

;

$result

= loadTxtDataIntoDatabase(

$splitChar

,

$file

,

$table

,

$conn

,

$fields

,

$insertType

);

if

(

array_shift

(

$result

)){

echo

'Success!<br/>'

;

}

else

{

echo

'Failed!--Error:'

.

array_shift

(

$result

).

'<br/>'

;

}

//调用示例3

require

'db.php'

;

$splitChar

=

'  '

//Tab

$file

=

'sqldata3.txt'

;

$fields

=

array

(

'id'

,

'value'

);

$table

=

'notExist'

//不存在表

$result

= loadTxtDataIntoDatabase(

$splitChar

,

$file

,

$table

,

$conn

,

$fields

);

if

(

array_shift

(

$result

)){

echo

'Success!<br/>'

;

}

else

{

echo

'Failed!--Error:'

.

array_shift

(

$result

).

'<br/>'

;

}

//附:db.php

复制代码 -- 数据表结构: -- 100000_insert,1000000_insert ?

1 2 3 4 5 6

CREATE

TABLE

`100000_insert` (

`id`

int

(11)

NOT

NULL

AUTO_INCREMENT,

`parentid`

int

(11)

NOT

NULL

,

`

name

`

varchar

(255)

DEFAULT

NULL

,

PRIMARY

KEY

(`id`)

) ENGINE=InnoDB AUTO_INCREMENT=1

DEFAULT

CHARSET=utf8

 100000 (10万)行插入:Insert 100000_line_data use 2.5534288883209 seconds  1000000(100万)行插入:Insert 1000000_line_data use 19.677318811417 seconds

版权声明:本文为CSDN博主「luyaran」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/luyaran/article/details/70318637

更多相关推荐

PHP读取txt文件到数据库

php 数据库 <?PHP$txt=$C->SITE_URL.'images/my.txt';$row=file($txt);//读出文件中内容到一个数组当中$num=0;//统计表中的记录数for($i=0;$i<count($row);$i++)//开始导入记录{ $fields=explode(";",$row[$i]);//读取数据到数....

php读取txt并写入数据库,php读取txt文件组成SQL并插入数据库的代码

php读取txt并写入数据库 php读取txt文件组成SQL并插入数据库的代码发布于2014-12-1408:16:49|129次阅读|评论:0|来源:网友投递PHP开源脚本语言PHP(外文名:HypertextPreprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收...

php读取txt并写入数据库,php读取txt文件组成SQL并插入数据库的方法

php读取txt并写入数据库 /***$splitChar字段分隔符*$file数据文件文件名*$table数据库表名*$conn数据库连接*$fields数据对应的列名*$insertType插入操作类型,包括INSERT,REPLACE*搜集整理:bbs.it-home.org*/functionloadTxtDataIntoDataba...

PHP 从数据库读取写入txt文件

php 2019独角兽企业重金招聘Python工程师标准>>>来源:http://bbs.csdn.net/topics/310002267转载于:https://my.oschina.net/273579540/blog/137256

Java读取csv文件并将内容插入到数据库

csv Java读取csv文件,并使用jdbc将内容插入到数据库,插入数据库的类可以在另一篇文章中查看,有一个公共的csv操作jar包,本文未使用<dependency><groupId>net.sourceforge.javacsv</groupId><artifactId>javacsv</arti...

    猜您喜欢

  • txt文件内容,插入到数据库。

  • PHP导入TXT文件数据到数据库

  • Python 读取txt插入数据库写入json到txt

  • PHP 读取大文件 和 插入数据库

  • php mysql json 读取_PHP接收json,并将接收数据插入...

    文章随机推荐

  • java中的死锁

  • 正则里的 /x7f-/xff 这样东西

  • 在bt5安装vmware tools

  • 同步方法和同步块,哪种更好_测试人员和开发人员之间更...

  • Spark基于Standalone及Yarn提交任务的执行分析

  • 移植U-Boot到BF548-EZKIT上(网卡DM9000EP)

  • ASP复仇商业软件 IBM成下一个目标

  • package bufio: unrecognized import path "bufio" 编...

  • B2B行业网站电话销售如何处理客户不接电话

  • Posix API与网络协议栈实现原理

  • 贪心算法---背包问题(物品可以分割问题)

  • 王建硕的2001交大Microsoft Club宣讲会

  • 问题以及解决------WPFDataGrid后台设置单元格背景色...

  • php 5.3.5 安装_PHP下载V5.3.27 官方安装版-PHP5.3版...

  • $(document).ready、body.Onload()和 $(window).load...

  • 首页

  • 技术博客

  • 联系我们

  • 版权申明

  • 隐私条款

© 2023 All rights reserved by CodeAntenna.com.