溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php怎么讀取txt文件組成SQL并插入數(shù)據(jù)庫(kù)

發(fā)布時(shí)間:2021-09-14 09:48:13 來(lái)源:億速云 閱讀:114 作者:chen 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“php怎么讀取txt文件組成SQL并插入數(shù)據(jù)庫(kù)”,在日常操作中,相信很多人在php怎么讀取txt文件組成SQL并插入數(shù)據(jù)庫(kù)問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”php怎么讀取txt文件組成SQL并插入數(shù)據(jù)庫(kù)”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

/**
* $splitChar 字段分隔符
* $file 數(shù)據(jù)文件文件名
* $table 數(shù)據(jù)庫(kù)表名
* $conn 數(shù)據(jù)庫(kù)連接
* $fields 數(shù)據(jù)對(duì)應(yīng)的列名
* $insertType 插入操作類(lèi)型,包括INSERT,REPLACE
*/

復(fù)制代碼 代碼如下:


<?php
/**
* $splitChar 字段分隔符
* $file 數(shù)據(jù)文件文件名
* $table 數(shù)據(jù)庫(kù)表名
* $conn 數(shù)據(jù)庫(kù)連接
* $fields 數(shù)據(jù)對(duì)應(yīng)的列名
* $insertType 插入操作類(lèi)型,包括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)

復(fù)制代碼 代碼如下:


-- 數(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萬(wàn))行插入:Insert 100000_line_data use 2.5534288883209 seconds
1000000(100萬(wàn))行插入:Insert 1000000_line_data use 19.677318811417 seconds
//可能報(bào)錯(cuò):MySQL server has gone away
//解決:修改my.ini/my.cnf max_allowed_packet=20M

到此,關(guān)于“php怎么讀取txt文件組成SQL并插入數(shù)據(jù)庫(kù)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI