PHP変換SQLiteデータベースはMySQL
ネット上でいくつかのツールを探して、しかしすべて単独で1つのデータテーブルをエクスポートして、しかもフォーマットは少し異なって、また手動でいくつかの文字を置き換えて、mysqlフォーマットに適することができます
だから自分で簡単に変えました
しかし、ここではTEXTタイプをvarchar(255)、INTEGERをint(11)に変換するなど、変換が粗いです.
学生が必要なら、自分で直してもいいです.
効果:
だから自分で簡単に変えました
しかし、ここではTEXTタイプをvarchar(255)、INTEGERをint(11)に変換するなど、変換が粗いです.
学生が必要なら、自分で直してもいいです.
<?php
class SQLiteToMySQL {
private $db;
private $pdo;
private $master_table_array = array();
private $replaceArray = array(
'"'=>'`',
'AUTOINCREMENT'=>'AUTO_INCREMENT',
'INTEGER'=>'int(11)',
'TEXT'=>'varchar(255)',
);
private $sqlDumpText;
public function __construct($db) {
$this->db = $db;
$this->open();
$this->getMasterData();
}
public function __toString() {
$str = 'SQLite MySQL。';
return $str;
}
private function open() {
if (!is_file($this->db)) {
throw new Exception($this->db.' 。');
}
$this->pdo = new PDO('sqlite:'.$this->db);
}
public function getMasterData() {
$res = $this->pdo->query('select * from sqlite_master');
$list = $res->fetchAll(PDO::FETCH_ASSOC);
$this->master_table_array = $list;
return $this;
}
/**
* SQL
* @param $arr
* @return string
*/
private function mergeSqlFields($arr) {
if(!$arr) return;
$result = '';
foreach ($arr as $k => $v) {
$result .= "'".$v."',";
}
return substr($result,0,strlen($result)-1);
}
/**
* SQL
* @param string $tb
* @return array
*/
public function getInsertSql($tb='') {
$resultArray = array();
foreach ($this->master_table_array as $k => $v) {
$table = $v['tbl_name'];
if ($tb) {
if ($tb != $table) continue;
}
$res = $this->pdo->query('select * from '.$table.' ');
$list = $res->fetchAll(PDO::FETCH_ASSOC);
foreach ($list as $k => $v) {
$mergeSqlFields = $this->mergeSqlFields($v);
$resultArray[$table][] = "INSERT INTO `".$table."` VALUES (".$mergeSqlFields.");";
}
}
return $resultArray;
}
/**
*
* @param string $tb
* @return array
*/
public function getCreateSql($tb='') {
$resultArray = array();
foreach ($this->master_table_array as $k => $v) {
$table = $v['tbl_name'];
if ($tb) {
if ($tb != $table) continue;
}
$resultArray[$table] = $v['sql'];
foreach ($this->replaceArray as $k => $v) {
$resultArray[$table] = str_replace($k,$v,$resultArray[$table]);
}
$resultArray[$table] = "DROP TABLE IF EXISTS `".$table."`;
".$resultArray[$table].'ENGINE=MyISAM DEFAULT CHARSET=utf8;';
}
return $resultArray;
}
/**
* sql
* @return $this
*/
public function getDumpSql() {
$this->sqlDumpText = '';
foreach ($this->master_table_array as $k => $v) {
$table = $v['tbl_name'];
if ($table == 'sqlite_sequence') continue;
$getCreateSql = $this->getCreateSql($table);
$getCreateSql = $getCreateSql[$table];
$getInsertSql = $this->getInsertSql($table);
$getInsertSql = implode("
",$getInsertSql[$table]);
$this->sqlDumpText .="
-- ----------------------------
-- Table structure for {$table}
-- ----------------------------
";
$this->sqlDumpText .=$getCreateSql;
$this->sqlDumpText .="
-- ----------------------------
-- Records for {$table}
-- ----------------------------
";
$this->sqlDumpText .=$getInsertSql;
}
return $this;
}
public function writeFile($savepath) {
if (!is_dir($dir = dirname($savepath))) {
mkdir($dir, 0777, true);
}
if ( !$hd = fopen($savepath, 'w')) {
throw new Exception(' :'.$savepath.' 。');
}
if (!fwrite($hd, $this->sqlDumpText)) {
throw new Exception(' 。');
}
fclose($hd);
return true;
}
public function printSql() {
echo $this->sqlDumpText;
return $this;
}
}
try {
//SQLite
$db = '/data/www/test/Database/data.db';
// sql
$savepath = '/data/www/test/MySQL/data.sql';
$SQLiteToMySQL = new SQLiteToMySQL($db);
if ($SQLiteToMySQL->getDumpSql()->writeFile($savepath)) {
echo PHP_EOL,' SQL ! :'.$savepath;
}
} catch (Exception $e) {
echo $e->getMessage();
}
効果:
-- ----------------------------
-- Table structure for menu
-- ----------------------------
DROP TABLE IF EXISTS `menu`;
CREATE TABLE `menu` (
`id` int(11) PRIMARY KEY AUTO_INCREMENT NOT NULL,
`type` int(11),
`pid` int(11),
`name` varchar(255),
`url` varchar(255),
`show` int(11) DEFAULT 1,
`sort` int(11)
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records for menu
-- ----------------------------
INSERT INTO `menu` VALUES ('7','2','0',' ','','0','0');
INSERT INTO `menu` VALUES ('8','2','7',' ','g=Admin&a=Menu&m=add','1','2');
INSERT INTO `menu` VALUES ('9','2','7',' ','g=Admin&a=Menu&m=index','1','1');
INSERT INTO `menu` VALUES ('10','2','0',' ','','0','0');
INSERT INTO `menu` VALUES ('11','2','10',' ','g=Admin&a=Game&m=add','1','0');
INSERT INTO `menu` VALUES ('12','2','10',' ','g=Admin&a=Game&m=index','1','0');
-- ----------------------------
-- Table structure for platform
-- ----------------------------
DROP TABLE IF EXISTS `platform`;
CREATE TABLE `platform` (
`id` int(11) PRIMARY KEY AUTO_INCREMENT NOT NULL,
`pid` int(11),
`code` int(11),
`name` varchar(255),
`sort` int(11)
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records for platform
-- ----------------------------
INSERT INTO `platform` VALUES ('1','16','qq','qq','');
INSERT INTO `platform` VALUES ('2','16','51','51','');
INSERT INTO `platform` VALUES ('4','16','baidu','baidu','');
INSERT INTO `platform` VALUES ('7','16','duowan','duowan','');