Home Backend Development PHP Tutorial php reads txt file and inserts data into database

php reads txt file and inserts data into database

Jul 29, 2016 am 09:02 AM
array connect table

今天测试一个功能,需要往数据库中插入一些原始数据,PM给了一个txt文件,如何快速的将这个txt文件的内容拆分为所要的数组,然后再插入到数据库中?

serial_number.txt的示例内容:

serial_number.txt:

DM00001A11 0116,
SN00002A11 0116,
AB00003A11 0116,
PV00004A11 0116,
OC00005A11 0116,
IX00006A11 0116,
Copy after login

创建数据表:

create table serial_number(
id int primary key auto_increment not null,
serial_number varchar(50) not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

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");
$c//explode()函数以","为标识符进行拆分

foreach ($contents as $k => $v)//遍历循环
{
  $id = $k;
  $serial_number = $v;
  mysql_query("insert into serial_number (`id`,`serial_number`)
      VALUES('$id','$serial_number')");
}

Copy after login

备注:方法有很多种,我这里是在拆分txt文件为数组后,然后遍历循环得到的数组,每循环一次,往数据库中插入一次。

再给大家分享一个支持大文件导入的

<&#63;php
/**
 * $splitChar 字段分隔符
 * $file 数据文件文件名
 * $table 数据库表名
 * $conn 数据库连接
 * $fields 数据对应的列名
 * $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('";  //数据头
  $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/>';
}
/*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
*/

//调用示例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
*/

//调用示例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 
*/

//调用示例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
/*  //注释这一行可全部释放
?>
<&#63;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;
}
&#63;>

Copy after login

//*/
复制代码
-- 数据表结构:

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

Copy after login

 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

以上就介绍了php读取txt文件并将数据插入到数据库,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

ftp_ssl_connect() function in PHP ftp_ssl_connect() function in PHP Aug 29, 2023 am 08:45 AM

The ftp_ssl_connect() function opens a secure SSL-FTP connection. Syntaxftp_ssl_connect(host,port,timeout); parameter host−FTP server address. It can be a domain name address or an IP address. port−The port to connect to. The default is 21. timeout − timeout for network operations. Return value The ftp_ssl_connect() function returns the SSL-FTP stream on success and FALSE on error. Example The following is an example −&lt;?ph

Detailed explanation of PHP array_fill() function usage Detailed explanation of PHP array_fill() function usage Jun 27, 2023 am 08:42 AM

In PHP programming, array is a very important data structure that can handle large amounts of data easily. PHP provides many array-related functions, array_fill() is one of them. This article will introduce in detail the usage of the array_fill() function, as well as some tips in practical applications. 1. Overview of the array_fill() function The function of the array_fill() function is to create an array of a specified length and composed of the same values. Specifically, the syntax of this function is

How to use vue3 table component How to use vue3 table component May 12, 2023 pm 09:40 PM

Basic table Before developing the table component, first think about what style of API to use. Because the author uses element in production work, the styles of the previous components are similar to element, but this time I do not plan to use the element style. , I plan to change it and display it directly: We expect users to use it like this: constdataList=[{id:1,name:'"JavaEE Enterprise Application Practice"',author:'dev1ce',price:'10.22',desc:&# 3

How to use the Array module in Python How to use the Array module in Python May 01, 2023 am 09:13 AM

The array module in Python is a predefined array, so it takes up much less space in memory than a standard list, and can also perform fast element-level operations such as adding, deleting, indexing, and slicing. In addition, all elements in the array are of the same type, so you can use the efficient numerical operation functions provided by the array, such as calculating the average, maximum, and minimum values. In addition, the array module also supports writing and reading array objects directly into binary files, which makes it more efficient when processing large amounts of numerical data. Therefore, if you need to process a large amount of homogeneous data, you may consider using Python's array module to optimize the execution efficiency of your code. To use the array module, you first need to

What are the common causes of ArrayStoreException in Java? What are the common causes of ArrayStoreException in Java? Jun 25, 2023 am 09:48 AM

In Java programming, array is an important data structure. Arrays can store multiple values ​​in a single variable, and more importantly each value can be accessed using an index. But while working with arrays, some exceptions may occur, one of them is ArrayStoreException. This article will discuss common causes of ArrayStoreException exceptions. 1. Type mismatch The element type must be specified when the array is created. When we try to store incompatible data types into an array, it throws

See all articles