如何在 Codeigniter 中将所有数据从一个数据库克隆到另一个数据库
How clone all data from one database to another one in Codeigniter
我要将所有数据从现场舞台数据库移动到另一个新数据库。
该项目基于 Codeignitor。
现在我正在使用转储文件,但处理它们真的很慢。
$temp_path = './db/dump/old_backup.sql';
// Create temporary table
echo "Preparing temporary database ..... \n";
$this->createTemporarDB();
// Make Dump from live db
echo "Preparing backup from live database ..... \n";
$backup = $this->exportDumpFromDB('default', 'old_backup.sql');
write_file($temp_path, $backup, 'w+');
$this->new_db = $this->load->database('new_db', true);
$temp_line = '';
$lines = file('./db/dump/old_backup.sql');
foreach ($lines as $line) {
if (substr($line, 0, 2) == '--' || $line == '' || substr($line, 0, 1) == '#') {
continue;
}
$temp_line .= $line;
// Display percentage of temp data
if (substr(trim($line), -1, 1) == ';') {
$this->temp_db->query($temp_line);
$temp_line = '';
}
$index++;
}
有什么好的方法可以解决我的问题(延迟问题,我需要加快速度)。
谢谢。
我解决了这个问题。
附上我的方法。
$tables = $this->db->query("SHOW TABLES FROM [old db name]")->result_array();
$this->temp_db = $this->load->database[new db name]', true);
foreach ($tables as $key => $val) {
$this->temp_db->query('CREATE TABLE ' . $val['Tables_in_squirrel'] . ' LIKE [old db name].' . $val['Tables_in_squirrel']);
$this->temp_db->query('INSERT ' . $val['Tables_in_[old db name]'] . ' SELECT * FROM squirrel.' . $val['Tables_in_squirrel']);
}
}
这是基于 Codeigniter 的代码。
我要将所有数据从现场舞台数据库移动到另一个新数据库。 该项目基于 Codeignitor。 现在我正在使用转储文件,但处理它们真的很慢。
$temp_path = './db/dump/old_backup.sql';
// Create temporary table
echo "Preparing temporary database ..... \n";
$this->createTemporarDB();
// Make Dump from live db
echo "Preparing backup from live database ..... \n";
$backup = $this->exportDumpFromDB('default', 'old_backup.sql');
write_file($temp_path, $backup, 'w+');
$this->new_db = $this->load->database('new_db', true);
$temp_line = '';
$lines = file('./db/dump/old_backup.sql');
foreach ($lines as $line) {
if (substr($line, 0, 2) == '--' || $line == '' || substr($line, 0, 1) == '#') {
continue;
}
$temp_line .= $line;
// Display percentage of temp data
if (substr(trim($line), -1, 1) == ';') {
$this->temp_db->query($temp_line);
$temp_line = '';
}
$index++;
}
有什么好的方法可以解决我的问题(延迟问题,我需要加快速度)。 谢谢。
我解决了这个问题。 附上我的方法。
$tables = $this->db->query("SHOW TABLES FROM [old db name]")->result_array();
$this->temp_db = $this->load->database[new db name]', true);
foreach ($tables as $key => $val) {
$this->temp_db->query('CREATE TABLE ' . $val['Tables_in_squirrel'] . ' LIKE [old db name].' . $val['Tables_in_squirrel']);
$this->temp_db->query('INSERT ' . $val['Tables_in_[old db name]'] . ' SELECT * FROM squirrel.' . $val['Tables_in_squirrel']);
}
}
这是基于 Codeigniter 的代码。