├── CHANGELOG.md ├── README.md ├── index.html └── src ├── download.php ├── generate.php └── transfer.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # convert-mysql-to-sqlite CHANGELOG 2 | 3 | ## 2.1.1 (2014-08-09) 4 | - Plugin name was changed. 5 | (プラグイン名、ファイル名、フォルダ名を変更しました。) 6 | 7 | ## v2.1 (2012-07-26) 8 | - The error when using "phpMyAdmin" is corrected. 9 | (phpMyAdminでダンプしたSQLの変換で、数行とばして処理してしまうことがある不具合を修正しました。) 10 | 11 | ## v2.0 (2012-07-20) 12 | - You can use the escaped "back-quote" and the escaped "new-line" in database. 13 | (エスケープされたバッククオート、改行文字に対応しました。) 14 | 15 | ## v1.1 (2012-07-17) 16 | - Stability of operation was improved. 17 | (動作の安定性を高めました。) 18 | 19 | ## v1.0 (2012-07-14) 20 | - The source code was exhibited newly. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # convert-mysql-to-sqlite 2 | Convert a MySQL dump file to a sql file for SQLite3 using PHP. 3 | 4 | ## Demo 5 | https://sutara79-php.herokuapp.com/demo/convert-mysql-to-sqlite/ 6 | 7 | **DO NOT** upload important files. 8 | If you want to do it, please download this source code and run in your local PHP environment. 9 | 10 | ## Usage in local 11 | Please regulate the following php.ini directives. 12 | 13 | - [memory_limit](//php.net/manual/en/ini.core.php#ini.memory-limit) 14 | - [post_max_size](//php.net/manual/en/ini.core.php#ini.post-max-size) 15 | - [upload_max_filesize](//php.net/manual/en/ini.core.php#ini.upload-max-filesize) 16 | 17 | You must set the value according to this rule. 18 | ``` 19 | memory_limit > post_max_size > upload_max_filesize 20 | ``` 21 | 22 | (exapmle) 23 | ```ini 24 | memory_limit = 128M 25 | post_max_size = 8M 26 | upload_max_filesize = 2M 27 | ``` 28 | 29 | ## License 30 | [MIT License](http://www.opensource.org/licenses/mit-license.php) 31 | 32 | 33 | ## Author 34 | [Yuusaku Miyazaki](http://d.hatena.ne.jp/sutara_lumpur/20120714/1342269933) 35 | ( ) 36 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Convert MySQL to SQLite 6 | 7 | 8 |

Convert MySQL to SQLite

9 | 15 |

16 | Upload a MySQL dump file.
17 | Then you will get a sql file for SQLite3. 18 |

19 |
20 |
21 | 22 |
23 |
24 | 25 | 26 | -------------------------------------------------------------------------------- /src/download.php: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 45 | 47 | 48 | 49 |

Back

50 | filesize = '.filesize($upfile).''; 60 | } else { 61 | echo 'Failed to send a file.'; 62 | } 63 | } else { 64 | echo 'File is not selected.'; 65 | } 66 | ?> 67 | 68 | 69 | -------------------------------------------------------------------------------- /src/transfer.php: -------------------------------------------------------------------------------- 1 | false, 51 | 'pos' => $_GET['pos'] 52 | ); 53 | fseek($fp1, $return['pos']); 54 | 55 | // 1度に処理するのは100行まで。 56 | // 57 | for ($i=0; ($line = fgets($fp1)) && ($i < 100); $i++) { 58 | // INSERT文の処理 59 | // phpMyAdminによるダンプの場合 60 | if (preg_match('/^(INSERT INTO \`[^\`]+\` )[\s\S]*VALUES$/ui', $line, $matches)) { 61 | $i++; 62 | $line = fgets($fp1); 63 | 64 | while (preg_match('/^([\s\S]+),$/u', $line, $matches2)) { 65 | $line = $matches[1].'VALUES'.$matches2[1].";\n"; 66 | my_fwrite($fp2, $line); 67 | 68 | //次のループのための準備 69 | //※ ループの条件に適さないと、1行余分に読み込んだことになる。 70 | //このため、CREATE文よりも先にこの処理を記述しなければならない。 71 | $i++; 72 | $line = fgets($fp1); 73 | } 74 | if (preg_match('/;$/u', $line)) { 75 | $line = $matches[1].'VALUES'.$line."\n"; 76 | my_fwrite($fp2, $line); 77 | } 78 | //INSERT文が終わったら、INSERT文以外の変換作業へ移る。 79 | } 80 | // 端末その他によるダンプの場合 81 | elseif (preg_match('/^(INSERT INTO \`[^\`]+\` VALUES\s?)([\s\S]*\);)$/ui', $line, $matches)) { 82 | while (preg_match_all('/^(\((?:(?:\'(?:(?:(?!\\\\).)?(?:(?:\\\\\\\\)*\\\\)\'|[^\'])*\'|[0-9]+|NULL),? ?)+\)), ?([\s\S]*)/ui', $matches[2], $matches2)) { 83 | $matches[2] = $matches2[2]; 84 | $line = $matches[1].$matches2[1].";\n"; 85 | my_fwrite($fp2, $line); 86 | } 87 | $line = $matches[1].$matches[2]."\n"; 88 | my_fwrite($fp2, $line); 89 | } 90 | 91 | // CREATE文の処理 92 | if (preg_match('/^CREATE TABLE[\s\S]*(\`[^\`]+\`)/ui', $line, $matches)) { 93 | my_fwrite($fp2, "CREATE TABLE {$matches[1]} (\n"); 94 | 95 | $i++; 96 | $line = fgets($fp1); 97 | 98 | $j = 0; 99 | while (!preg_match('/^\) ENGINE=/i', $line)) { 100 | 101 | // int系をintegerへ、AUTO_INCREMENTを削除する 102 | if (!preg_match('/^\s*UNIQUE KEY/ui', $line)) { 103 | // 以下の置換で行末のカンマを削除している。CREATE文が続くなら、カンマを復活させる。 104 | if ($j > 0) fwrite($fp2, ",\n"); 105 | $j++; 106 | 107 | $line = preg_replace_callback('/^(\s*`[^`]+`\s*)(int|tinyint|samllint|mediumint|bigint)(?:\([^\)]+\))?|(AUTO_INCREMENT),?\\n$|(,\\n)$/ui', 'callback_create', $line); 108 | my_fwrite($fp2, $line); 109 | } 110 | $i++; 111 | $line = fgets($fp1); 112 | } 113 | // ") ENGINE=" の文は、ENGINE以降を削除 114 | fwrite($fp2, "\n);\n"); 115 | $flag_create_table = false; 116 | } 117 | $return['pos'] = ftell($fp1); 118 | } 119 | $return['feof'] = feof($fp1); 120 | fclose($fp1); 121 | fclose($fp2); 122 | echo json_encode($return); --------------------------------------------------------------------------------