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...
文章随機推薦 -
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.
|