首页 > 数据库技术 > 详细

CI框架 数据库批量插入 insert_batch()

时间:2015-07-31 18:09:34      阅读:10627      评论:0      收藏:0      [点我收藏+]
  • 使用CI框架的AR操作:insert_batch()可以减少访问数据库的次数。一次访问即可。

 

示例1:

$data
= array( array( ‘title‘ => ‘My title‘ , ‘name‘ => ‘My Name‘ , ‘date‘ => ‘My date‘ ), array( ‘title‘ => ‘Another title‘ , ‘name‘ => ‘Another Name‘ , ‘date‘ => ‘Another date‘ ) ); $this->db->insert_batch(‘mytable‘, $data); //生成: INSERT INTO mytable (title, name, date) VALUES (‘My title‘, ‘My name‘, ‘My date‘), (‘Another title‘, ‘Another name‘, ‘Another date‘)

 

 

 

示例2:

$one_info
= array(); $insert_data = array(); $one_info[‘role_id‘] = 6; $one_info[‘operator‘] = ‘test‘; for($i = 0; $i <= 3; $i++) { $one_info[‘net_id‘] = $i; $insert_data[] = $one_info; } if (!$this->db->insert_batch(tableA,$insert_data)) { return 3; }

//插入的sql语句是 insert into tableA(role_id,operator,net_id) values(6,‘test‘,0),(6,‘test‘,1),(6,‘test‘,2);

注意:第一个参数包含表名,第二个是一个包含数据的关联数组。

 

CI框架 数据库批量插入 insert_batch()

原文:http://www.cnblogs.com/longzhongren/p/4692557.html

(1)
(2)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!