当前位置:Gxlcms > PHP教程 > php读取txt文件组成SQL并插入数据库的方法

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

时间:2021-07-01 10:21:17 帮助过:28人阅读

  1. /**

  2. * $splitChar 字段分隔符
  3. * $file 数据文件文件名
  4. * $table 数据库表名
  5. * $conn 数据库连接
  6. * $fields 数据对应的列名
  7. * $insertType 插入操作类型,包括INSERT,REPLACE
  8. * 搜集整理:bbs.it-home.org
  9. */
  10. function loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields=array(),$insertType='INSERT'){
  11. if(empty($fields)) $head = "{$insertType} INTO `{$table}` VALUES('";
  12. else $head = "{$insertType} INTO `{$table}`(`".implode('`,`',$fields)."`) VALUES('"; //数据头
  13. $end = "')";
  14. $sqldata = trim(file_get_contents($file));
  15. if(preg_replace('/\s*/i','',$splitChar) == '') {
  16. $splitChar = '/(\w+)(\s+)/i';
  17. $replace = "$1','";
  18. $specialFunc = 'preg_replace';
  19. }else {
  20. $splitChar = $splitChar;
  21. $replace = "','";
  22. $specialFunc = 'str_replace';
  23. }
  24. //处理数据体,二者顺序不可换,否则空格或Tab分隔符时出错
  25. $sqldata = preg_replace('/(\s*)(\n+)(\s*)/i','\'),(\'',$sqldata); //替换换行
  26. $sqldata = $specialFunc($splitChar,$replace,$sqldata); //替换分隔符
  27. $query = $head.$sqldata.$end; //数据拼接
  28. if(mysql_query($query,$conn)) return array(true);
  29. else {
  30. return array(false,mysql_error($conn),mysql_errno($conn));
  31. }
  32. }
  33. //调用示例1
  34. require 'db.php';
  35. $splitChar = '|'; //竖线
  36. $file = 'sqldata1.txt';
  37. $fields = array('id','parentid','name');
  38. $table = 'cengji';
  39. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
  40. if (array_shift($result)){
  41. echo 'Success!
    ';
  42. }else {
  43. echo 'Failed!--Error:'.array_shift($result).'
    ';
  44. }
  45. /*sqlda ta1.txt
  46. |0|A
  47. |1|B
  48. |1|C
  49. |2|D
  50. -- cengji
  51. CREATE TABLE `cengji` (
  52. `id` int(11) NOT NULL AUTO_INCREMENT,
  53. `parentid` int(11) NOT NULL,
  54. `name` varchar(255) DEFAULT NULL,
  55. PRIMARY KEY (`id`),
  56. UNIQUE KEY `parentid_name_unique` (`parentid`,`name`) USING BTREE
  57. ) ENGINE=InnoDB AUTO_INCREMENT=1602 DEFAULT CHARSET=utf8
  58. */
  59. //调用示例2
  60. require 'db.php';
  61. $splitChar = ' '; //空格
  62. $file = 'sqldata2.txt';
  63. $fields = array('id','make','model','year');
  64. $table = 'cars';
  65. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
  66. if (array_shift($result)){
  67. echo 'Success!
    ';
  68. }else {
  69. echo 'Failed!--Error:'.array_shift($result).'
    ';
  70. }
  71. /* sqldata2.txt
  72. Aston DB19 2009
  73. Aston DB29 2009
  74. Aston DB39 2009
  75. -- cars
  76. CREATE TABLE `cars` (
  77. `id` int(11) NOT NULL AUTO_INCREMENT,
  78. `make` varchar(16) NOT NULL,
  79. `model` varchar(16) DEFAULT NULL,
  80. `year` varchar(16) DEFAULT NULL,
  81. PRIMARY KEY (`id`)
  82. ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
  83. */
  84. //调用示例3
  85. require 'db.php';
  86. $splitChar = ' '; //Tab
  87. $file = 'sqldata3.txt';
  88. $fields = array('id','make','model','year');
  89. $table = 'cars';
  90. $insertType = 'REPLACE';
  91. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields,$insertType);
  92. if (array_shift($result)){
  93. echo 'Success!
    ';
  94. }else {
  95. echo 'Failed!--Error:'.array_shift($result).'
    ';
  96. }
  97. /* sqldata3.txt
  98. Aston DB19 2009
  99. Aston DB29 2009
  100. Aston DB39 2009
  101. */
  102. //调用示例3
  103. require 'db.php';
  104. $splitChar = ' '; //Tab
  105. $file = 'sqldata3.txt';
  106. $fields = array('id','value');
  107. $table = 'notExist'; //不存在表
  108. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
  109. if (array_shift($result)){
  110. echo 'Success!
    ';
  111. }else {
  112. echo 'Failed!--Error:'.array_shift($result).'
    ';
  113. }

  114. //附:db.php

  115. /* //注释这一行可全部释放
  116. ?>
  117. static $connect = null;
  118. static $table = 'jilian';
  119. if(!isset($connect)) {
  120. $connect = mysql_connect("localhost","root","");
  121. if(!$connect) {
  122. $connect = mysql_connect("localhost","Zjmainstay","");
  123. }
  124. if(!$connect) {
  125. die('Can not connect to database.Fatal error handle by /test/db.php');
  126. }
  127. mysql_select_db("test",$connect);
  128. mysql_query("SET NAMES utf8",$connect);
  129. $conn = &$connect;
  130. $db = &$connect;
  131. }
  132. ?>

代码如下:

  1. //*/

  2. 数据表结构

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

作者:Zjmainstay

人气教程排行