douzhang3822 2017-12-06 03:53
浏览 185

从Codeigniter备份还原数据库

I was trying to make an automated database backup using built-in CodeIgniter $this->dbutil->backup().

The script is:

    $this->load->dbutil();

    $backup = $this->dbutil->backup(array(
        'tables'        => array(),   // Array of tables to backup.
        'ignore'        => array('regencies', 'villages', 'provinces'),                     // List of tables to omit from the backup
        'add_drop'      => TRUE,                        // Whether to add DROP TABLE statements to backup file
        'add_insert'    => TRUE,                        // Whether to add INSERT data to backup file
        'newline'       => "
"                         // Newline character used in backup file
    ));

    $this->load->helper('file');

    $latest = md5(uniqid());

    write_file(APPPATH . 'backup/'. $latest .'.gz', $backup);

The scripts run just fine. But the problem comes when i wan't to restore. It comes like this:

sql result of the codeigniter backup

As a result of that i cannot restore it from CLI (mysql -u root -p dbname < db.sql) and from Navicat/Sequel Pro.

restore database from cli

The question is, how to restore that it to database again? (Notice that there's no quot after VALUES)

  • 写回答

2条回答 默认 最新

  • dozc58418381 2017-12-06 04:12
    关注

    This is a simple SQL syntax issue. String literals need to be enclosed in single quotes.

    INSERT INTO `foo` (`bar`,`bell`) VALUES ( Hey diddle diddle , cat and fiddle )
    

    won't work. We need

    INSERT INTO `foo` (`bar`,`bell`) VALUES ('Hey diddle diddle','cat and fiddle')
                                             ^-----------------^ ^--------------^
    

    Take a closer look at the actual SQL statement you are trying to execute.

    Your "unloader" is going to need to be smarter, enclosing string and date values in single quotes when they get put into a SQL statement. (For convenience, numeric literals can be enclosed in single quotes as well, so you don't need to discriminate, just enclose all values in the VALUES list in single quotes.

    Also be aware of what will happen when a value includes a single quote. For example:

     ... VALUES ( 'O'Leary' , ... 
                  ^_^     
    

    That's also going to be an error. Single quotes within a value will need to be escaped. One way to do that in MySQL is to precede a single quote with another single quote, like this:

     ... VALUES ( 'O''Leary' , ... 
                  ^--------^
    

    This would be valid, and would evaluate to a string containing a single single quote O'Leary

    Also, MySQL will interpret a backslash character in a string literal as an escape character. For example:

     ... VALUES ( 'this probably 
    ot what we want' , 
                                 ^^       
    

    That sequence is going to interpreted as a newline character, not as the character n. So, backslash characters are also going to need to be escaped.

    et al.

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试