PHP SQL File Import Class (example)

Source: Internet
Author: User
  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. Determine if a file exists
  17. if (!file_exists ($sqlPath))
  18. return false;
  19. $handle = fopen ($sqlPath, ' RB ');
  20. $SQLSTR = Fread ($handle, FileSize ($sqlPath));
  21. Splitting by a statement separator in SQL syntax
  22. $segment = Explode (";", Trim ($SQLSTR));
  23. Var_dump ($segment);
  24. Get rid of 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. Judging whether it would be 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 it is not a comment, it is considered an 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 table names are valid when the first row appears, such as CREATE table Talbename
  54. $regxTable = "^[\ \" \ "]{0,1}[\_a-za-z]+[\_a-za-z0-9]*[\" \ "]{0,1}$";//Regular expression for table name processing
  55. $regxLeftWall = "^[\" \ "\" Skiff ";
  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. Combining SQL statements
  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"); declaring character sets
  103. mysql_query ($sql);
  104. }
  105. Mysql_close ($conn);
  106. }
  107. Private Function Findtablename ($sqlFlagTree, $tokens, $tokensKey =0,& $tableName = Array ())
  108. {
  109. $regxLeftWall = "^[\" \ "\" Skiff ";
  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. }
Copy Code

2, call Method:

    1. $dbM = new DBManager (' localhost ', ' root ', ' root ', ' 6639 ');
    2. $dbM->createfromfile (' Data.sql ', null, ');
Copy Code
  • 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.