php读取txt文件组成SQL并插入数据库的方法

php中文网
发布: 2016-07-25 09:04:33
原创
1138人浏览过
  1. /**

  2. * $splitchar 字段分隔符
  3. * $file 数据文件文件名
  4. * $table 数据库表名
  5. * $conn 数据库连接
  6. * $fields 数据对应的列名
  7. * $inserttype 插入操作类型,包括insert,replace
  8. * 搜集整理:bbs.it-home.org
  9. */
  10. function loadtxtdataintodatabase($splitchar,$file,$table,$conn,$fields=array(),$inserttype='insert'){
  11. if(empty($fields)) $head = "{$inserttype} into `{$table}` values('";
  12. else $head = "{$inserttype} into `{$table}`(`".implode('`,`',$fields)."`) values('"; //数据头
  13. $end = "')";
  14. $sqldata = trim(file_get_contents($file));
  15. if(preg_replace('/\s*/i','',$splitchar) == '') {
  16. $splitchar = '/(\w+)(\s+)/i';
  17. $replace = "$1','";
  18. $specialfunc = 'preg_replace';
  19. }else {
  20. $splitchar = $splitchar;
  21. $replace = "','";
  22. $specialfunc = 'str_replace';
  23. }
  24. //处理数据体,二者顺序不可换,否则空格或tab分隔符时出错
  25. $sqldata = preg_replace('/(\s*)(\n+)(\s*)/i','\'),(\'',$sqldata); //替换换行
  26. $sqldata = $specialfunc($splitchar,$replace,$sqldata); //替换分隔符
  27. $query = $head.$sqldata.$end; //数据拼接
  28. if(mysql_query($query,$conn)) return array(true);
  29. else {
  30. return array(false,mysql_error($conn),mysql_errno($conn));
  31. }
  32. }
  33. //调用示例1
  34. require 'db.php';
  35. $splitchar = '|'; //竖线
  36. $file = 'sqldata1.txt';
  37. $fields = array('id','parentid','name');
  38. $table = 'cengji';
  39. $result = loadtxtdataintodatabase($splitchar,$file,$table,$conn,$fields);
  40. if (array_shift($result)){
  41. echo 'success!
    ';
  42. }else {
  43. echo 'failed!--error:'.array_shift($result).'
    ';
  44. }
  45. /*sqlda ta1.txt
  46. |0|a
  47. |1|b
  48. |1|c
  49. |2|d
  50. -- cengji
  51. create table `cengji` (
  52. `id` int(11) not null auto_increment,
  53. `parentid` int(11) not null,
  54. `name` varchar(255) default null,
  55. primary key (`id`),
  56. unique key `parentid_name_unique` (`parentid`,`name`) using btree
  57. ) engine=innodb auto_increment=1602 default charset=utf8
  58. */
  59. //调用示例2
  60. require 'db.php';
  61. $splitchar = ' '; //空格
  62. $file = 'sqldata2.txt';
  63. $fields = array('id','make','model','year');
  64. $table = 'cars';
  65. $result = loadtxtdataintodatabase($splitchar,$file,$table,$conn,$fields);
  66. if (array_shift($result)){
  67. echo 'success!
    ';
  68. }else {
  69. echo 'failed!--error:'.array_shift($result).'
    ';
  70. }
  71. /* sqldata2.txt
  72. aston db19 2009
  73. aston db29 2009
  74. aston db39 2009
  75. -- cars
  76. create table `cars` (
  77. `id` int(11) not null auto_increment,
  78. `make` varchar(16) not null,
  79. `model` varchar(16) default null,
  80. `year` varchar(16) default null,
  81. primary key (`id`)
  82. ) engine=innodb auto_increment=14 default charset=utf8
  83. */
  84. //调用示例3
  85. require 'db.php';
  86. $splitchar = ' '; //tab
  87. $file = 'sqldata3.txt';
  88. $fields = array('id','make','model','year');
  89. $table = 'cars';
  90. $inserttype = 'replace';
  91. $result = loadtxtdataintodatabase($splitchar,$file,$table,$conn,$fields,$inserttype);
  92. if (array_shift($result)){
  93. echo 'success!
    ';
  94. }else {
  95. echo 'failed!--error:'.array_shift($result).'
    ';
  96. }
  97. /* sqldata3.txt
  98. aston db19 2009
  99. aston db29 2009
  100. aston db39 2009
  101. */
  102. //调用示例3
  103. require 'db.php';
  104. $splitchar = ' '; //tab
  105. $file = 'sqldata3.txt';
  106. $fields = array('id','value');
  107. $table = 'notexist'; //不存在表
  108. $result = loadtxtdataintodatabase($splitchar,$file,$table,$conn,$fields);
  109. if (array_shift($result)){
  110. echo 'success!
    ';
  111. }else {
  112. echo 'failed!--error:'.array_shift($result).'
    ';
  113. }
  114. //附:db.php

  115. /* //注释这一行可全部释放
  116. ?>
  117. static $connect = null;
  118. static $table = 'jilian';
  119. if(!isset($connect)) {
  120. $connect = mysql_connect("localhost","root","");
  121. if(!$connect) {
  122. $connect = mysql_connect("localhost","Zjmainstay","");
  123. }
  124. if(!$connect) {
  125. die('Can not connect to database.Fatal error handle by /test/db.php');
  126. }
  127. mysql_select_db("test",$connect);
  128. mysql_query("SET NAMES utf8",$connect);
  129. $conn = &$connect;
  130. $db = &$connect;
  131. }
  132. ?>
复制代码

复制代码 代码如下:

  1. //*/

  2. 数据表结构

    小绿鲸英文文献阅读器
    小绿鲸英文文献阅读器

    英文文献阅读器,专注提高SCI阅读效率

    小绿鲸英文文献阅读器 199
    查看详情 小绿鲸英文文献阅读器
  3. -- 数据表结构:
  4. -- 100000_insert,1000000_insert
  5. CREATE TABLE `100000_insert` (
  6. `id` int(11) NOT NULL AUTO_INCREMENT,
  7. `parentid` int(11) NOT NULL,
  8. `name` varchar(255) DEFAULT NULL,
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
  11. 100000 (10万)行插入:Insert 100000_line_data use 2.5534288883209 seconds
  12. 1000000(100万)行插入:Insert 1000000_line_data use 19.677318811417 seconds
  13. //可能报错:MySQL server has gone away
  14. //解决:修改my.ini/my.cnf max_allowed_packet=20M

    立即学习PHP免费学习笔记(深入)”;

复制代码

作者:Zjmainstay



PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号