首页 > 开发 > PHP > 正文

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

2024-05-04 22:01:46
字体:大 中 小
来源:转载
供稿:网友

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

serial_number.txt的示例内容:

serial_number.txt:

  1. DM00001A11 0116, 
  2.  
  3. SN00002A11 0116, 
  4.  
  5. AB00003A11 0116, 
  6.  
  7. PV00004A11 0116, 
  8.  
  9. OC00005A11 0116, 
  10.  
  11. IX00006A11 0116, 

创建数据表:

  1. create table serial_number( 
  2.  
  3. id int primary key auto_increment not null, 
  4.  
  5. serial_number varchar(50) not null 
  6.  
  7. )ENGINE=InnoDB DEFAULT CHARSET=utf8; 

php代码如下:

  1. $conn = mysql_connect('127.0.0.1','root','') or die("Invalid query: " . mysql_error()); 
  2.  
  3. mysql_select_db('test', $conn) or die("Invalid query: " . mysql_error()); 
  4.  
  5. $content = file_get_contents("serial_number.txt"); 
  6.  
  7. $contents= explode(",",$content);//explode()函数以","为标识符进行拆分 
  8.  
  9. foreach ($contents as $k => $v)//遍历循环 
  10.  
  11. { 
  12.  
  13.   $id = $k; 
  14.  
  15.   $serial_number = $v; 
  16.  
  17.   mysql_query("insert into serial_number (`id`,`serial_number`) 
  18.  
  19.       VALUES('$id','$serial_number')"); 
  20.  
  21. } 

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

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

  1. <?php 
  2.  
  3. /** 
  4.  
  5.  * $splitChar 字段分隔符 
  6.  
  7.  * $file 数据文件文件名 
  8.  
  9.  * $table 数据库表名 
  10.  
  11.  * $conn 数据库连接 
  12.  
  13.  * $fields 数据对应的列名 
  14.  
  15.  * $insertType 插入操作类型,包括INSERT,REPLACE 
  16.  
  17.  */ 
  18.  
  19. function loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields=array(),$insertType='INSERT'){ 
  20.  
  21.   if(emptyempty($fields)) $head = "{$insertType} INTO `{$table}` VALUES('"; 
  22.  
  23.   else $head = "{$insertType} INTO `{$table}`(`".implode('`,`',$fields)."`) VALUES('";  //数据头 
  24.  
  25.   $end = "')"; 
  26.  
  27.   $sqldata = trim(file_get_contents($file)); 
  28.  
  29.   if(preg_replace('//s*/i','',$splitChar) == '') { 
  30.  
  31.     $splitChar = '/(/w+)(/s+)/i'; 
  32.  
  33.     $replace = "$1','"; 
  34.  
  35.     $specialFunc = 'preg_replace'; 
  36.  
  37.   }else { 
  38.  
  39.     $splitChar = $splitChar; 
  40.  
  41.     $replace = "','"; 
  42.  
  43.     $specialFunc = 'str_replace'; 
  44.  
  45.   } 
  46.  
  47.   //处理数据体,二者顺序不可换,否则空格或Tab分隔符时出错 
  48.  
  49.   $sqldata = preg_replace('/(/s*)(/n+)(/s*)/i','/'),(/'',$sqldata);  //替换换行 
  50.  
  51.   $sqldata = $specialFunc($splitChar,$replace,$sqldata);        //替换分隔符 
  52.  
  53.   $query = $head.$sqldata.$end;  //数据拼接 
  54.  
  55.   if(mysql_query($query,$conn)) return array(true); 
  56.  
  57.   else { 
  58.  
  59.     return array(false,mysql_error($conn),mysql_errno($conn)); 
  60.  
  61.   } 
  62.  
  63. } 
  64.  
  65.  
  66.  
  67. //调用示例1 
  68.  
  69. require 'db.php'; 
  70.  
  71. $splitChar = '|';  //竖线 
  72.  
  73. $file = 'sqldata1.txt'; 
  74.  
  75. $fields = array('id','parentid','name'); 
  76.  
  77. $table = 'cengji'; 
  78.  
  79. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields); 
  80.  
  81. if (array_shift($result)){ 
  82.  
  83.   echo 'Success!<br>'; 
  84.  
  85. }else { 
  86.  
  87.   echo 'Failed!--Error:'.array_shift($result).'<br>'; 
  88.  
  89. } 
  90.  
  91. /*sqlda ta1.txt 
  92.  
  93. 1|0|A 
  94.  
  95. 2|1|B 
  96.  
  97. 3|1|C 
  98.  
  99. 4|2|D 
  100.  
  101.  
  102.  
  103. -- cengji 
  104.  
  105. CREATE TABLE `cengji` ( 
  106.  
  107.  `id` int(11) NOT NULL AUTO_INCREMENT, 
  108.  
  109.  `parentid` int(11) NOT NULL, 
  110.  
  111.  `name` varchar(255) DEFAULT NULL, 
  112.  
  113.  PRIMARY KEY (`id`), 
  114.  
  115.  UNIQUE KEY `parentid_name_unique` (`parentid`,`name`) USING BTREE 
  116.  
  117. ) ENGINE=InnoDB AUTO_INCREMENT=1602 DEFAULT CHARSET=utf8 
  118.  
  119. */ 
  120.  
  121.  
  122.  
  123. //调用示例2 
  124.  
  125. require 'db.php'; 
  126.  
  127. $splitChar = ' ';  //空格 
  128.  
  129. $file = 'sqldata2.txt'; 
  130.  
  131. $fields = array('id','make','model','year'); 
  132.  
  133. $table = 'cars'; 
  134.  
  135. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields); 
  136.  
  137. if (array_shift($result)){ 
  138.  
  139.   echo 'Success!<br>'; 
  140.  
  141. }else { 
  142.  
  143.   echo 'Failed!--Error:'.array_shift($result).'<br>'; 
  144.  
  145. } 
  146.  
  147. /* sqldata2.txt 
  148.  
  149. 11 Aston DB19 2009 
  150.  
  151. 12 Aston DB29 2009 
  152.  
  153. 13 Aston DB39 2009 
  154.  
  155.  
  156.  
  157. -- cars 
  158.  
  159. CREATE TABLE `cars` ( 
  160.  
  161.  `id` int(11) NOT NULL AUTO_INCREMENT, 
  162.  
  163.  `make` varchar(16) NOT NULL, 
  164.  
  165.  `model` varchar(16) DEFAULT NULL, 
  166.  
  167.  `year` varchar(16) DEFAULT NULL, 
  168.  
  169.  PRIMARY KEY (`id`) 
  170.  
  171. ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 
  172.  
  173. */ 
  174.  
  175.  
  176.  
  177. //调用示例3 
  178.  
  179. require 'db.php'; 
  180.  
  181. $splitChar = '  ';  //Tab 
  182.  
  183. $file = 'sqldata3.txt'; 
  184.  
  185. $fields = array('id','make','model','year'); 
  186.  
  187. $table = 'cars'; 
  188.  
  189. $insertType = 'REPLACE'; 
  190.  
  191. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields,$insertType); 
  192.  
  193. if (array_shift($result)){ 
  194.  
  195.   echo 'Success!<br>'; 
  196.  
  197. }else { 
  198.  
  199.   echo 'Failed!--Error:'.array_shift($result).'<br>'; 
  200.  
  201. } 
  202.  
  203. /* sqldata3.txt 
  204.  
  205. 11  Aston  DB19  2009 
  206.  
  207. 12  Aston  DB29  2009 
  208.  
  209. 13  Aston  DB39  2009  
  210.  
  211. */ 
  212.  
  213.  
  214.  
  215. //调用示例3 
  216.  
  217. require 'db.php'; 
  218.  
  219. $splitChar = '  ';  //Tab 
  220.  
  221. $file = 'sqldata3.txt'; 
  222.  
  223. $fields = array('id','value'); 
  224.  
  225. $table = 'notExist';  //不存在表 
  226.  
  227. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields); 
  228.  
  229. if (array_shift($result)){ 
  230.  
  231.   echo 'Success!<br>'; 
  232.  
  233. }else { 
  234.  
  235.   echo 'Failed!--Error:'.array_shift($result).'<br>'; 
  236.  
  237. } 
  238.  
  239.  
  240.  
  241. //附:db.php 
  242.  
  243. /*  //注释这一行可全部释放 
  244.  
  245. ?> 
  246.  
  247. <?php 
  248.  
  249. static $connect = null; 
  250.  
  251. static $table = 'jilian'; 
  252.  
  253. if(!isset($connect)) { 
  254.  
  255.   $connect = mysql_connect("localhost","root",""); 
  256.  
  257.   if(!$connect) { 
  258.  
  259.     $connect = mysql_connect("localhost","Zjmainstay",""); 
  260.  
  261.   } 
  262.  
  263.   if(!$connect) { 
  264.  
  265.     die('Can not connect to database.Fatal error handle by /test/db.php'); 
  266.  
  267.   } 
  268.  
  269.   mysql_select_db("test",$connect); 
  270.  
  271.   mysql_query("SET NAMES utf8",$connect); 
  272.  
  273.   $conn = &$connect; 
  274.  
  275.   $db = &$connect; 
  276.  
  277. } 
  278.  
  279. ?> 

复制代码,-- 数据表结构:

  1. -- 100000_insert,1000000_insert 
  2.  
  3. CREATE TABLE `100000_insert` ( 
  4.  
  5.  `id` int(11) NOT NULL AUTO_INCREMENT, 
  6.  
  7.  `parentid` int(11) NOT NULL, 
  8.  
  9.  `name` varchar(255) DEFAULT NULL, 
  10.  
  11.  PRIMARY KEY (`id`) 
  12.  
  13. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 
  14.  
  15. 100000 (10万)行插入:Insert 100000_line_data use 2.5534288883209 seconds 
  16.  
  17. 1000000(100万)行插入:Insert 1000000_line_data use 19.677318811417 seconds 
  18.  
  19. //可能报错:MySQL server has gone away 
  20.  
  21. //解决:修改my.ini/my.cnf max_allowed_packet=20M 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表