欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

文件導(dǎo)入數(shù)據(jù)庫(kù)php 文件導(dǎo)入數(shù)據(jù)庫(kù)JAVA

如何利用php讀取txt文件再將數(shù)據(jù)插入到數(shù)據(jù)庫(kù)

serial_number.txt的示例內(nèi)容:

創(chuàng)新互聯(lián)是一家專(zhuān)業(yè)提供海鹽企業(yè)網(wǎng)站建設(shè),專(zhuān)注與成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為海鹽眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專(zhuān)業(yè)的建站公司優(yōu)惠進(jìn)行中。

serial_number.txt:

DM00001A11 0116,

SN00002A11 0116,

AB00003A11 0116,

PV00004A11 0116,

OC00005A11 0116,

IX00006A11 0116,

創(chuàng)建數(shù)據(jù)表:

create table serial_number(

id int primary key auto_increment not null,

serial_number varchar(50) not null

)ENGINE=InnoDB DEFAULT CHARSET=utf8;

php代碼如下:

$conn = mysql_connect('127.0.0.1','root','') or die("Invalid query: " . mysql_error());

mysql_select_db('test', $conn) or die("Invalid query: " . mysql_error());

$content = file_get_contents("serial_number.txt");

$contents= explode(",",$content);//explode()函數(shù)以","為標(biāo)識(shí)符進(jìn)行拆分

foreach ($contents as $k = $v)//遍歷循環(huán)

{

$id = $k;

$serial_number = $v;

mysql_query("insert into serial_number (`id`,`serial_number`)

VALUES('$id','$serial_number')");

}

備注:方法有很多種,我這里是在拆分txt文件為數(shù)組后,然后遍歷循環(huán)得到的數(shù)組,每循環(huán)一次,往數(shù)據(jù)庫(kù)中插入一次。

再給大家分享一個(gè)支持大文件導(dǎo)入的

?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 = '|'; //豎線(xiàn)

$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

1|0|A

2|1|B

3|1|C

4|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

11 Aston DB19 2009

12 Aston DB29 2009

13 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

11 Aston DB19 2009

12 Aston DB29 2009

13 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):

-- 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

php如何讀取CSV大文件并且將其導(dǎo)入數(shù)據(jù)庫(kù)示例

思路:

讀取csv文件,每讀取一行數(shù)據(jù),就插入數(shù)據(jù)庫(kù)

示例

文件夾結(jié)構(gòu)

/

file.csv????//csv大文件,這里只模擬三行數(shù)據(jù),不考慮運(yùn)行效率(PS:csv文件格式很簡(jiǎn)單,文件一般較小,解析很快,運(yùn)行效率的瓶頸主要在寫(xiě)入數(shù)據(jù)庫(kù)操作)

index.php????//php文件

file.csv

singi,20

lily,19

daming,23

index.php

/**

*?讀取csv文件,每讀取一行數(shù)據(jù),就插入數(shù)據(jù)庫(kù)

*/

//獲取數(shù)據(jù)庫(kù)實(shí)例

$dsn?=?'mysql:dbname=test;host=127.0.0.1';

$user?=?'root';

$password?=?'';

try?{

$db?=?new?PDO($dsn,?$user,?$password);

}?catch?(PDOException?$e)?{

echo?'Connection?failed:?'?.?$e-getMessage();

}

//讀取file.csv文件

if?(($handle?=?fopen("file.csv",?"r"))?!==?FALSE)?{

while?(($row?=?fgetcsv($handle,?1000,?","))?!==?FALSE)?{

//寫(xiě)入數(shù)據(jù)庫(kù)

$sth?=?$db-prepare('insert?into?test?set?name=:name,age=:age');

$sth-bindParam(':name',$row[0],PDO::PARAM_STR,255);

$sth-bindParam(':age',$row[1],PDO::PARAM_INT);

$sth-execute();

}

fclose($handle);

}

數(shù)據(jù)表

CREATE?TABLE?`test`?(

`id`?INT(10)?UNSIGNED?NOT?NULL?AUTO_INCREMENT,

`name`?VARCHAR(255)?NULL?DEFAULT?''?COLLATE?'utf8mb4_bin',

`age`?INT(10)?NULL?DEFAULT?'0',

PRIMARY?KEY?(`id`)

)

COLLATE='utf8mb4_bin'

ENGINE=InnoDB;

運(yùn)行結(jié)束后,數(shù)據(jù)庫(kù)中會(huì)插入csv中的三行數(shù)據(jù)

PHP 用PHPExcel往數(shù)據(jù)庫(kù)導(dǎo)入大量數(shù)據(jù)

1、首先我們準(zhǔn)備一個(gè)含有數(shù)據(jù)的Excel表格,表頭和數(shù)據(jù)表中的表字段相對(duì)應(yīng)。

2、在ThinkPHP中引入PHPExcel類(lèi)庫(kù)。

3、然后我們編寫(xiě)導(dǎo)入的PHP代碼。

4、然后我們編寫(xiě)導(dǎo)出的PHP代碼。

5、然后我們進(jìn)行導(dǎo)出測(cè)試發(fā)現(xiàn)可以導(dǎo)出即可。

php路徑導(dǎo)入是什么意思

就是把事先有的數(shù)據(jù)或文件上傳到你的應(yīng)用程序或網(wǎng)上。

導(dǎo)入的方法:點(diǎn)擊MySQL管理器。

點(diǎn)擊phpmyadmin就沒(méi)有在網(wǎng)頁(yè)上登陸數(shù)據(jù)庫(kù)操作導(dǎo)入導(dǎo)出了。

點(diǎn)擊MySQL管理器,導(dǎo)入導(dǎo)出這,彈出對(duì)話(huà)框。

要輸入數(shù)據(jù)庫(kù)密碼,然后寫(xiě)好要導(dǎo)出的數(shù)據(jù)庫(kù)名,點(diǎn)擊導(dǎo)出就可以了。

就會(huì)導(dǎo)出跳到導(dǎo)出數(shù)據(jù)庫(kù)的文件夾中。

在軟件文件夾和www同級(jí)的目錄有個(gè)backup文件夾。導(dǎo)出的就在里面。

導(dǎo)入也是一樣,選擇文件,導(dǎo)入到指定的數(shù)據(jù)庫(kù)中。

當(dāng)前文章:文件導(dǎo)入數(shù)據(jù)庫(kù)php 文件導(dǎo)入數(shù)據(jù)庫(kù)JAVA
網(wǎng)頁(yè)URL:http://chinadenli.net/article40/dooideo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)定制開(kāi)發(fā)網(wǎng)站維護(hù)用戶(hù)體驗(yàn)企業(yè)網(wǎng)站制作做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)