Php reads a txt file to form an SQL statement and inserts it into the database

Source: Internet
Author: User
Php reads a txt file to form an SQL statement and inserts it into the database

  1. /**

  2. * $ SplitChar field delimiter
  3. * $ File data file name
  4. * $ Table database table name
  5. * $ Conn database connection
  6. * $ Name of the column corresponding to fields data
  7. * $ InsertType INSERT operation type, including INSERT and REPLACE
  8. * Collect: bbs.it-home.org
  9. */
  10. Function loadtxtdata1_database ($ 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 ('"; // data header
  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. // Process the data body. The order of the two parts cannot be changed. Otherwise, an error occurs when the space or Tab separator is used.
  25. $ Sqldata = preg_replace ('/(\ s *) (\ n +) (\ s *)/I', '\'), (\ '', $ sqldata ); // replace line feed
  26. $ Sqldata = $ specialFunc ($ splitChar, $ replace, $ sqldata); // replace the delimiter
  27. $ Query = $ head. $ sqldata. $ end; // data stitching
  28. If (mysql_query ($ query, $ conn) return array (true );
  29. Else {
  30. Return array (false, mysql_error ($ conn), mysql_errno ($ conn ));
  31. }
  32. }
  33. // Call Example 1
  34. Require 'DB. php ';
  35. $ SplitChar = '|'; // vertical bar
  36. $ File = 'sqldata1.txt ';
  37. $ Fields = array ('id', 'parentid', 'name ');
  38. $ Table = 'cengji ';
  39. $ Result = loadtxtdata1_database ($ 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 |
  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. // Call Example 2
  60. Require 'DB. php ';
  61. $ SplitChar = ''; // Space
  62. $ File = 'sqldata2.txt ';
  63. $ Fields = array ('id', 'make', 'model', 'year ');
  64. $ Table = 'Cars ';
  65. $ Result = loadtxtdata1_database ($ 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. // Call Example 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 = loadtxtdata1_database ($ 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. // Call Example 3
  103. Require 'DB. php ';
  104. $ SplitChar = ''; // Tab
  105. $ File = 'sqldata3.txt ';
  106. $ Fields = array ('id', 'value ');
  107. $ Table = 'notexist'; // The table does not exist.
  108. $ Result = loadtxtdata1_database ($ splitChar, $ file, $ table, $ conn, $ fields );
  109. If (array_shift ($ result )){
  110. Echo 'Success!
    ';
  111. } Else {
  112. Echo 'failed! -- Error: '. array_shift ($ result ).'
    ';
  113. }

  114. // Appendix: db. php

  115. /* // Note that all rows can be released.
  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. ?>

The code is as follows:

  1. //*/

  2. Data table structure

  3. -- Data table structure:
  4. -- 100000_insert, 1_00_insert
  5. Create table '2017 _ 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. Insert 100000 (0.1 million) rows: Insert 100000_line_data use 2.5534288883209 seconds
  12. Insert 1000000 (1 million) rows: Insert into 00_line_data use 19.677318811417 seconds
  13. // Possible error: MySQL server has gone away
  14. // Solution: modify my. ini/my. cnf max_allowed_packet = 20 M

Author: Zjmainstay

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.