Home > Backend Development > PHP Tutorial > php sql file import class (example)

php sql file import class (example)

WBOY
Release: 2016-07-25 08:55:00
Original
889 people have browsed it
  1. class DBManager
  2. {
  3. var $dbHost = '';
  4. var $dbUser = '';
  5. var $dbPassword = '';
  6. var $dbSchema = '';
  7. function __construct ($host,$user,$password,$schema)
  8. {
  9. $this->dbHost = $host;
  10. $this->dbUser = $user;
  11. $this->dbPassword = $password;
  12. $ this->dbSchema = $schema;
  13. }
  14. function createFromFile($sqlPath,$delimiter = '(;n)|((;rn))|(;r)',$prefix = '',$commenter = array('#','--'))
  15. {
  16. //Determine whether the file exists
  17. if(!file_exists($sqlPath))
  18. return false;
  19. $handle = fopen($sqlPath,'rb');
  20. $sqlStr = fread($handle,filesize($sqlPath));
  21. //Split through the statement delimiter of sql syntax
  22. $segment = explode(";",trim($sqlStr));
  23. / /var_dump($segment);
  24. //Remove comments and extra blank lines
  25. foreach($segment as & $statement)
  26. {
  27. $sentence = explode("n",$statement);
  28. $newStatement = array ();
  29. foreach($sentence as $subSentence)
  30. {
  31. if('' != trim($subSentence))
  32. {
  33. //Judge whether it is a comment
  34. $isComment = false;
  35. foreach($commenter as $comer)
  36. {
  37. if(eregi("^(".$comer.")",trim($subSentence)))
  38. {
  39. $isComment = true;
  40. break;
  41. }
  42. }
  43. //if not Comment, it is considered to be a sql statement
  44. if(!$isComment)
  45. $newStatement[] = $subSentence;
  46. }
  47. }
  48. $statement = $newStatement;
  49. }
  50. //Prefix the table name
  51. if('' != $prefix)
  52. {
  53. //Only valid when the table name appears in the first row. For example, CREATE TABLE talbeName
  54. $regxTable = "^[`'"]{0,1}[_a-zA-Z ]+[_a-zA-Z0-9]*[`'"]{0,1}$";//Regular expression for processing table names
  55. $regxLeftWall = "^[`'"]A boat with leaves";
  56. $sqlFlagTree = array(
  57. "CREATE" => array(
  58. "TABLE" => array(
  59. "$regxTable" => 0
  60. )
  61. ),
  62. "INSERT" => array(
  63. " INTO" => array(
  64. "$regxTable" => 0
  65. )
  66. )
  67. );
  68. foreach($segment as & $statement)
  69. {
  70. $tokens = split(" ",$statement[0 ]);
  71. $tableName = array();
  72. $this->findTableName($sqlFlagTree,$tokens,0,$tableName);
  73. if(empty($tableName['leftWall']))
  74. {
  75. $newTableName = $prefix.$tableName['name'];
  76. }
  77. else{
  78. $newTableName = $tableName['leftWall'].$prefix.substr($tableName['name'],1);
  79. }
  80. $statement[0] = str_replace($tableName['name'],$newTableName,$statement[0]);
  81. }
  82. }
  83. //Combined sql statement
  84. foreach($segment as & $statement)
  85. {
  86. $newStmt = '';
  87. foreach($statement as $sentence)
  88. {
  89. $newStmt = $newStmt.trim($sentence)."n";
  90. }
  91. $statement = $newStmt;
  92. }
  93. self: :saveByQuery($segment);
  94. return true;
  95. }
  96. private function saveByQuery($sqlArray)
  97. {
  98. $conn = mysql_connect($this->dbHost,$this->dbUser,$this-> dbPassword);
  99. mysql_select_db($this->dbSchema);
  100. foreach($sqlArray as $sql)
  101. {
  102. mysql_query("set names utf8"); //Declare character set
  103. mysql_query($sql);
  104. }
  105. mysql_close($conn);
  106. }
  107. private function findTableName($sqlFlagTree,$tokens,$tokensKey=0,& $tableName = array())
  108. {
  109. $regxLeftWall = "^[`'"]一叶扁舟";
  110. if(count($tokens)<=$tokensKey)
  111. return false;
  112. if('' == trim($tokens[$tokensKey]))
  113. {
  114. return self::findTableName($sqlFlagTree,$tokens,$tokensKey+1,$tableName);
  115. }
  116. else
  117. {
  118. foreach($sqlFlagTree as $flag => $v)
  119. {
  120. if(eregi($flag,$tokens[$tokensKey]))
  121. {
  122. if(0==$v)
  123. {
  124. $tableName['name'] = $tokens[$tokensKey];
  125. if(eregi($regxLeftWall,$tableName['name']))
  126. {
  127. $tableName['leftWall'] = $tableName['name']{0};
  128. }
  129. return true;
  130. }
  131. else{
  132. return self::findTableName($v,$tokens,$tokensKey+1,& $tableName);
  133. }
  134. }
  135. }
  136. }
  137. return false;
  138. }
  139. }
  140. function writeArrayToFile($fileName,$dataArray,$delimiter="rn")
  141. {
  142. $handle=fopen($fileName, "wb");
  143. $text = '';
  144. foreach($dataArray as $data)
  145. {
  146. $text = $text.$data.$delimiter;
  147. }
  148. fwrite($handle,$text);
  149. }
复制代码

2,调用方法:

  1. $dbM = new DBManager('localhost','root','root','6639');
  2. $dbM->createFromFile(‘data.sql’,null,'');
复制代码


Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template