天天看點

php讀取txt并寫入資料庫,php讀取txt檔案組成SQL并插入資料庫的代碼

php讀取txt檔案組成SQL并插入資料庫的代碼

釋出于 2014-12-14 08:16:49 | 129 次閱讀 | 評論: 0 | 來源: 網友投遞

PHP開源腳本語言PHP(外文名: Hypertext Preprocessor,中文名:“超文本預處理器”)是一種通用開源腳本語言。文法吸收了C語言、Java和Perl的特點,入門門檻較低,易于學習,使用廣泛,主要适用于Web開發領域。PHP的檔案字尾名為php。

本文是一個php讀取txt檔案組成SQL并插入資料庫的代碼示例,感興趣的同學參考下.

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!

';

}else {

echo 'Failed!--Error:'.array_shift($result).'

';

}

//調用示例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!

';

}else {

echo 'Failed!--Error:'.array_shift($result).'

';

}

//調用示例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!

';

}else {

echo 'Failed!--Error:'.array_shift($result).'

';

}

//調用示例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!

';

}else {

echo 'Failed!--Error:'.array_shift($result).'

';

}

//附:db.php

資料表結構

-- 資料表結構:

-- 100000_insert,1000000_insert

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

//可能報錯:MySQL server has gone away

//解決:修改my.ini/my.cnf max_allowed_packet=20M

作者:Zjmainstay

相關閱讀:

php讀取txt檔案組成SQL并插入資料庫的代碼

php讀取txt檔案并将資料插入到資料庫

PHP讀取txt檔案的内容并指派給數組的代碼

PHP讀取TXT檔案向資料庫導入海量資料的方法

PHP讀取txt文本檔案并分頁顯示的方法

PHP 操作TXT檔案(打開/關閉/檢查/讀取)示例

php讀取csv檔案并輸出的方法

php讀取csc檔案并輸出

php逐行讀取txt檔案寫入數組的方法

PHP 讀取文本檔案内容并分頁顯示

php删除txt檔案指定行及按行讀取txt文檔資料的方法

PHP讀取文本檔案并逐行輸出該行使用最多的字元與對應次數的方法