1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | <?php 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/>' ; } //调用示例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/>' ; } //调用示例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/>' ; } //调用示例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
复制代码
-- 数据表结构:
-- 100000_insert,1000000_insert
? 1 2 3 4 5 6 | 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
版权声明:本文为CSDN博主「luyaran」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/luyaran/article/details/70318637
更多相关推荐 PHP读取txt文件到数据库
php
数据库
<?PHP$txt=$C->SITE_URL.'images/my.txt';$row=file($txt);//读出文件中内容到一个数组当中$num=0;//统计表中的记录数for($i=0;$i<count($row);$i++)//开始导入记录{ $fields=explode(";",$row[$i]);//读取数据到数....
php读取txt并写入数据库,php读取txt文件组成SQL并插入数据库的代码
php读取txt并写入数据库
php读取txt文件组成SQL并插入数据库的代码发布于2014-12-1408:16:49|129次阅读|评论:0|来源:网友投递PHP开源脚本语言PHP(外文名:HypertextPreprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收...
php读取txt并写入数据库,php读取txt文件组成SQL并插入数据库的方法
php读取txt并写入数据库
/***$splitChar字段分隔符*$file数据文件文件名*$table数据库表名*$conn数据库连接*$fields数据对应的列名*$insertType插入操作类型,包括INSERT,REPLACE*搜集整理:bbs.it-home.org*/functionloadTxtDataIntoDataba...
PHP 从数据库读取写入txt文件
php
2019独角兽企业重金招聘Python工程师标准>>>来源:http://bbs.csdn.net/topics/310002267转载于:https://my.oschina.net/273579540/blog/137256
Java读取csv文件并将内容插入到数据库
csv
Java读取csv文件,并使用jdbc将内容插入到数据库,插入数据库的类可以在另一篇文章中查看,有一个公共的csv操作jar包,本文未使用<dependency><groupId>net.sourceforge.javacsv</groupId><artifactId>javacsv</arti...
文章随机推荐 -
java中的死锁 -
正则里的 /x7f-/xff 这样东西 -
在bt5安装vmware tools -
同步方法和同步块,哪种更好_测试人员和开发人员之间更... -
Spark基于Standalone及Yarn提交任务的执行分析 -
移植U-Boot到BF548-EZKIT上(网卡DM9000EP) -
ASP复仇商业软件 IBM成下一个目标 -
package bufio: unrecognized import path "bufio" 编... -
B2B行业网站电话销售如何处理客户不接电话 -
Posix API与网络协议栈实现原理 -
贪心算法---背包问题(物品可以分割问题) -
王建硕的2001交大Microsoft Club宣讲会 -
问题以及解决------WPFDataGrid后台设置单元格背景色... -
php 5.3.5 安装_PHP下载V5.3.27 官方安装版-PHP5.3版... -
$(document).ready、body.Onload()和 $(window).load...
© 2023 All rights reserved by CodeAntenna.com.
|