繁体   English   中英

如何在Codeigniter批处理插入查询中添加INSERT IGNORE INTO

[英]How to add INSERT IGNORE INTO in Codeigniter batch insert query

如何使codeigniter函数与insert_batch()相同,但生成类似INSERT IGNORE INTO的查询?

我需要INSERT IGNORE INTO因为表的键之一是唯一的,并且在出现重复输入时显示错误。

我在codeigniter批量插入查询中搜索了添加“ INSERT IGNORE INTO”而不是“ INSERT INTO”的代码,但没有找到结果。 是的,我们可以使用PHP登录名进行自定义批量插入查询,但是如果您想在codeigniter中使用此功能,则可以使用该功能。

在(codeigniter / system / database / DB_active.rec.php)中添加此功能。

/*
*Function For Batch Insert using Ignore Into
*/
public function custom_insert_batch($table = '', $set = NULL)
{
    if ( ! is_null($set))
    {
        $this->set_insert_batch($set);
    }

    if (count($this->ar_set) == 0)
    {
        if ($this->db_debug)
        {
            //No valid data array.  Folds in cases where keys and values did not match up
            return $this->display_error('db_must_use_set');
        }
        return FALSE;
    }

    if ($table == '')
    {
        if ( ! isset($this->ar_from[0]))
        {
            if ($this->db_debug)
            {
                return $this->display_error('db_must_set_table');
            }
            return FALSE;
        }

        $table = $this->ar_from[0];
    }

    // Batch this baby
    for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
    {

        $sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
        $sql = str_replace('INSERT INTO','INSERT IGNORE INTO',$sql);
        //echo $sql;

        $this->query($sql);
    }

    $this->_reset_write();


    return TRUE;
}

使用此功能

$this->db->custom_insert_batch($table_name, $batch_data);

谢谢 !

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM