Php 如何通过dbutil导入codeigniter导出的sql文件
我导出了数据库并将文件保存在文件夹中,现在我想使用codeigniter在mysql数据库中导入相同的文件,下面是我的导出方法,该方法工作正常,但现在我想导入相同的文件Php 如何通过dbutil导入codeigniter导出的sql文件,php,codeigniter,Php,Codeigniter,我导出了数据库并将文件保存在文件夹中,现在我想使用codeigniter在mysql数据库中导入相同的文件,下面是我的导出方法,该方法工作正常,但现在我想导入相同的文件 function getFeeRelatedTableBackups() { $this->load->dbutil(); $this->load->helper('url'); $this->load->helper('file');
function getFeeRelatedTableBackups()
{
$this->load->dbutil();
$this->load->helper('url');
$this->load->helper('file');
$this->load->helper('download');
$this->load->library('zip');
$prefs = array(
'tables' => array('table1', 'table2', 'table3'), // Array of tables to backup.
'ignore' => array(), // List of tables to omit from the backup
'format' => 'sql', // gzip, zip, txt
'filename' => 'mybackup.sql', // File name - NEEDED ONLY WITH ZIP FILES
'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file
'add_insert' => TRUE, // Whether to add INSERT data to backup file
'newline' => "\n" // Newline character used in backup file
);
$backup=& $this->dbutil->backup($prefs);
$dbname='backup-on-'.date('Y-m-d-h:i:s').'.sql';
$save=UPLOAD_PATH_FEE_MANAGEMENT_TABLES.$dbname;
write_file($save,$backup);
force_download($dbname,$backup);
}
尝试了这两种恢复方法,但两种方法都不起作用
function restoredb()
{
$isi_file = file_get_contents(UPLOAD_PATH_FEE_MANAGEMENT_TABLES.'backup-on-2018-02-12-07:57:37.sql');
$string_query = rtrim( $isi_file, "\n;" );
$array_query = explode(";", $query);
foreach($array_query as $query)
{
$this->db->query($query);
}
}
public function import_database()
{
$temp_line = '';
$lines = file(UPLOAD_PATH_FEE_MANAGEMENT_TABLES.'backup-on-2018-02-12-07:57:37.sql');
foreach ($lines as $line)
{
if (substr($line, 0, 2) == '--' || $line == '')
continue;
$temp_line .= $line;
if (substr(trim($line), -1, 1) == ';')
{
$this->db->query($temp_line);
$temp_line = '';
}
}
}
试着这样做:
public function import_database() {
$temp_line = '';
$lines = file('/path/to/file/my_file.sql');
foreach ($lines as $line)
{
if (substr($line, 0, 2) == '--' || $line == '' || substr($line, 0, 1) == '#')
continue;
$temp_line .= $line;
if (substr(trim($line), -1, 1) == ';')
{
$this->db->query($temp_line);
$temp_line = '';
}
}
}
我认为问题不在于评论或其他格式问题。 阅读
$this->dbutil->backup()
的输出时,以下内容非常适合我
与Shoukat回答的主要区别是
simple\u query()
而不是query()
,因为后者的功能不是必需的public function restoredb($file)
{
if (file_exists($file))
{
$lines = file($file);
$statement = '';
foreach ($lines as $line)
{
$statement .= $line;
if (substr(trim($line), -1) === ';')
{
$this->db->simple_query($statement);
$statement = '';
}
}
}
}
您可以使用
file()
读取.sql文件的内容,然后生成一个字符串。构建字符串后,执行$this->db->query($builtString)代码>我认为问题在于:发票的##表结构#如果存在,则删除表发票
;创建表invoice
(invoice\u id
int(11)非空自动增量,凭证号
varchar(30)校对utf8\u unicode\u ci默认为空,class\u id
int(11)默认的NULLall代码显示在同一行中,我想是在注释所有查询,如何替换它?您需要在循环中处理if条件下的if(substr($line,0,2)='-'|$line='''''.|$line=''|$line='.\35;')
if(substr($line,0,2)='-'.$line=''-'.\124; substr($line,0,1)='.