首頁  >  文章  >  後端開發  >  php讀取txt檔案組成SQL並插入資料庫的方法

php讀取txt檔案組成SQL並插入資料庫的方法

WBOY
WBOY原創
2016-07-25 09:04:33928瀏覽
  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,$,,$file,$ $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. `id`),
  56. UNIQUE KEY `parentid_name_unique` (`parentid`,`name`) USING BTREE
  57. ) ENGINE=InnoDB AUTO_INCREMENT=1602 DEFAULT CHARSET=utf8
  58. /require 'db.php';
  59. $splitChar = ' '; //空格
  60. $file = 'sqldata2.txt';
  61. $fields = array('id','make' ,'model','year');
  62. $table = 'cars';
  63. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
  64. if (array_shift ($result)){
  65. echo 'Success!
    ';
  66. }else {
  67. echo 'Failed!--Error:'.array_shift($result).'
    ' ;
  68. }
  69. /* sqldata2.txt
  70. Aston DB19 2009
  71. Aston DB29 2009
  72. Aston DB39 2009
  73. -- cars
  74. Aston DB39 2009
  75. -- cars
  76. Aston DB39 2009
  77. -- cars
  78. ATE`37> id` int(11) NOT NULL AUTO_INCREMENT,
  79. `make` varchar(16) NOT NULL,
  80. `model` varchar(16) DEFAULT NULL,
  81. `year` varchar(16)PRIMARY KEY (`id`)
  82. ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
  83. */
  84. //呼叫範例3
  85. require 'db.php'; splitChar = ' '; //Tab
  86. $file = 'sqldata3.txt';
  87. $fields = array('id','make','model','year');
  88. $table = 'cars';
  89. $insertType = 'REPLACE';
  90. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields,$insertType);
  91. if (array_shift($ result)){
  92. echo 'Success!
    ';
  93. }else {
  94. echo 'Failed!--Error:'.array_shift($result).'
    ';
  95. }
  96. /* sqldata3.txt
  97. Aston DB19 2009
  98. Aston DB29 2009
  99. Aston DB39 2009
  100. */
  101. ';
  102. $splitChar = ' '; //Tab
  103. $file = 'sqldata3.txt';
  104. $fields = array('id','value');
  105. $table = ' notExist'; //不存在表格
  106. $result = loadTxtDataIntoDatabase($splitChar,$file,$table,$conn,$fields);
  107. if (array_shift($result)){
  108. echo 'Success !
    ';
  109. }else {
  110. echo 'Failed!--Error:'.array_shift($result).'
    ';
  111. }
  112. //附:db.php

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

複製程式碼 程式碼如下:

  1. //*/

  2. 資料表結構
  3. -- 資料表結構:
  4. -- 100000_insert,1000000_insert
  5. CREATE TABLE `100000_insert` (
  6. `id` int(11) NOT ` int(11) NOT NULL,
  7. `name` varchar(255) DEFAULT NULL,
  8. PRIMARY KEY (`id`)
  9. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utv81000000(100萬)行插入:Insert 1000000_line_data 00019.673178173172 月/>sec3100/0067310 away
  10. //解決:修改my. ini/my.cnf max_allowed_pa​​cket=20M
  11. 複製代碼
作者:Zjmainstay

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn