php讀取txt文件組成SQL并插入數(shù)據(jù)庫的代碼(原創(chuàng)自Zjmainstay)
* $splitChar 字段分隔符
* $file 數(shù)據(jù)文件文件名
* $table 數(shù)據(jù)庫表名
* $conn 數(shù)據(jù)庫連接
* $fields 數(shù)據(jù)對(duì)應(yīng)的列名
* $insertType 插入操作類型,包括INSERT,REPLACE
*/
<?php
/**
* $splitChar 字段分隔符
* $file 數(shù)據(jù)文件文件名
* $table 數(shù)據(jù)庫表名
* $conn 數(shù)據(jù)庫連接
* $fields 數(shù)據(jù)對(duì)應(yīng)的列名
* $insertType 插入操作類型,包括INSERT,REPLACE
*/
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('"; //數(shù)據(jù)頭
$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';
}
//處理數(shù)據(jù)體,二者順序不可換,否則空格或Tab分隔符時(shí)出錯(cuò)
$sqldata = preg_replace('/(\s*)(\n+)(\s*)/i','\'),(\'',$sqldata); //替換換行
$sqldata = $specialFunc($splitChar,$replace,$sqldata); //替換分隔符
$query = $head.$sqldata.$end; //數(shù)據(jù)拼接
if(mysql_query($query,$conn)) return array(true);
else {
return array(false,mysql_error($conn),mysql_errno($conn));
}
}
//調(diào)用示例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/>';
}
/*sqlda ta1.txt
|0|A
|1|B
|1|C
|2|D
-- cengji
CREATE TABLE `cengji` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parentid` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `parentid_name_unique` (`parentid`,`name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1602 DEFAULT CHARSET=utf8
*/
//調(diào)用示例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/>';
}
/* sqldata2.txt
Aston DB19 2009
Aston DB29 2009
Aston DB39 2009
-- cars
CREATE TABLE `cars` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`make` varchar(16) NOT NULL,
`model` varchar(16) DEFAULT NULL,
`year` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8
*/
//調(diào)用示例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/>';
}
/* sqldata3.txt
Aston DB19 2009
Aston DB29 2009
Aston DB39 2009
*/
//調(diào)用示例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
/* //注釋這一行可全部釋放
?>
<?php
static $connect = null;
static $table = 'jilian';
if(!isset($connect)) {
$connect = mysql_connect("localhost","root","");
if(!$connect) {
$connect = mysql_connect("localhost","Zjmainstay","");
}
if(!$connect) {
die('Can not connect to database.Fatal error handle by /test/db.php');
}
mysql_select_db("test",$connect);
mysql_query("SET NAMES utf8",$connect);
$conn = &$connect;
$db = &$connect;
}
?>
//*/
數(shù)據(jù)表結(jié)構(gòu)
-- 數(shù)據(jù)表結(jié)構(gòu):
-- 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
//可能報(bào)錯(cuò):MySQL server has gone away
//解決:修改my.ini/my.cnf max_allowed_packet=20M
作者:Zjmainstay
- php逐行讀取txt文件寫入數(shù)組的方法
- PHP讀取txt文件的內(nèi)容并賦值給數(shù)組的代碼
- PHP 讀取文件內(nèi)容代碼(txt,js等)
- php讀取txt文件并將數(shù)據(jù)插入到數(shù)據(jù)庫
- 基于PHP讀取TXT文件向數(shù)據(jù)庫導(dǎo)入海量數(shù)據(jù)的方法
- PHP 處理TXT文件(打開/關(guān)閉/檢查/讀取)
- php刪除txt文件指定行及按行讀取txt文檔數(shù)據(jù)的方法
- PHP讀取txt文本文件并分頁顯示的方法
- PHP讀取遠(yuǎn)程txt文檔到數(shù)組并實(shí)現(xiàn)遍歷
- PHP如何從txt文件中讀取數(shù)據(jù)詳解
相關(guān)文章
php編譯安裝php-amq擴(kuò)展簡(jiǎn)明教程
這篇文章主要介紹了php編譯安裝php-amq擴(kuò)展的方法,較為詳細(xì)的分析了php-amq擴(kuò)展的功能及下載、編譯安裝的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-06-06PHP實(shí)現(xiàn)更改hosts文件的方法示例
這篇文章主要介紹了PHP實(shí)現(xiàn)更改hosts文件的方法,結(jié)合具體實(shí)例形式分析了php操作hosts文件的相關(guān)讀取、設(shè)置、刪除等實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08PHP常用特殊運(yùn)算符號(hào)和函數(shù)總結(jié)(php新手入門必看)
新手經(jīng)常會(huì)有一些PHP特殊符號(hào)的問題,這里把常用的特殊符號(hào)整理一下。如果你全部都會(huì)用,那就當(dāng)是溫故知新吧2013-02-02php 計(jì)算兩個(gè)時(shí)間戳相隔的時(shí)間的函數(shù)(小時(shí))
計(jì)算兩個(gè)時(shí)間戳相隔的時(shí)間,以前腳本之家發(fā)布過具體到天數(shù)的,這個(gè)可以具體到小時(shí)數(shù),需要的朋友可以參考下。2009-12-12php+javascript實(shí)現(xiàn)的動(dòng)態(tài)顯示服務(wù)器運(yùn)行程序進(jìn)度條功能示例
這篇文章主要介紹了php+javascript實(shí)現(xiàn)的動(dòng)態(tài)顯示服務(wù)器運(yùn)行程序進(jìn)度條功能,涉及php結(jié)合javascript數(shù)學(xué)運(yùn)算與緩沖輸出相關(guān)操作技巧,需要的朋友可以參考下2017-08-08php實(shí)現(xiàn)將wav文件轉(zhuǎn)換成圖像文件并在頁面中顯示的方法
這篇文章主要介紹了php實(shí)現(xiàn)將wav文件轉(zhuǎn)換成圖像文件并在頁面中顯示的方法,涉及php中unpack、fopen、fread等方法及圖形操作的相關(guān)技巧,需要的朋友可以參考下2015-04-04php簡(jiǎn)單定時(shí)執(zhí)行任務(wù)的實(shí)現(xiàn)方法
這篇文章主要介紹了php簡(jiǎn)單定時(shí)執(zhí)行任務(wù)的實(shí)現(xiàn)方法,涉及curl及sleep等操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02php下實(shí)現(xiàn)在指定目錄搜索指定類型文件的函數(shù)
php在特定目錄中找特定類型的文件2008-10-10