当前位置:Gxlcms > PHP教程 > phpsql文件导入类(示例)

phpsql文件导入类(示例)

时间:2021-07-01 10:21:17 帮助过:22人阅读

  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)|((;\r\n))|(;\r)',$prefix = '',$commenter = array('#','--'))
  15. {
  16. //判断文件是否存在
  17. if(!file_exists($sqlPath))
  18. return false;
  19. $handle = fopen($sqlPath,'rb');
  20. $sqlStr = fread($handle,filesize($sqlPath));
  21. //通过sql语法的语句分割符进行分割
  22. $segment = explode(";",trim($sqlStr));
  23. //var_dump($segment);
  24. //去掉注释和多余的空行
  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. //判断是会否是注释
  34. $isComment = false;
  35. foreach($commenter as $comer)
  36. {
  37. if(eregi("^(".$comer.")",trim($subSentence)))
  38. {
  39. $isComment = true;
  40. break;
  41. }
  42. }
  43. //如果不是注释,则认为是sql语句
  44. if(!$isComment)
  45. $newStatement[] = $subSentence;
  46. }
  47. }
  48. $statement = $newStatement;
  49. }
  50. //对表名加前缀
  51. if('' != $prefix)
  52. {
  53. //只有表名在第一行出现时才有效 例如 CREATE TABLE talbeName
  54. $regxTable = "^[\`\'\"]{0,1}[\_a-zA-Z]+[\_a-zA-Z0-9]*[\`\'\"]{0,1}$";//处理表名的正则表达式
  55. $regxLeftWall = "^[\`\'\"]一叶扁舟";
  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. //组合sql语句
  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"); //声明字符集
  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="\r\n")
  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,'');

人气教程排行