2012-07-18 97 views
2

我必须在模型上运行该SQL例程:如何用codeigniter锁定表格?

$this->db->query('LOCK TABLE orders WRITE'); 
$this->db->query('TRUNCATE TABLE orders'); 
$this->db->query('INSERT INTO orders SELECT * FROM orders_tmp'); 
$this->db->query('UNLOCK TABLES'); 

,但我得到这个错误:

Error Number: 1192
Impossible to execute the requested command: tables under lock or transaction running
TRUNCATE TABLE orders

我使用MyISAM作为DB引擎上此表。

你能帮我吗?

回答

1

在表上real_table执行许多INSERT和SELECT操作时,并行插入是不可能的,你可以插入行到一个临时表temp_table,并周期性地从临时表中的行更新真正的表。这可以通过以下代码完成:

mysql> LOCK TABLES real_table WRITE, temp_table WRITE; 

请问是否它不适合您。

+0

这篇文章可以帮助你。 http://stackoverflow.com/questions/1951161/mysql-myisam-table-locking – 2012-07-18 12:21:51

+0

遗憾的是它不工作!第一条SQL我做的是:“LOCK TABLES命令WRITE,orders_tmp写”,但我得到一个错误,从CI,指出台的订单没有被锁定... :( – user1534715 2012-07-18 15:30:50

+0

它始终是更好地使用InnoDB引擎,而使用的表或行级锁定。不管怎么说,我会尽力尽快给您更具体的答案。谢谢 – 2012-07-19 04:00:49

1

试试这个

$this->db->query('TRUNCATE TABLE orders'); 
$this->db->query('LOCK TABLE orders WRITE'); 
$this->db->query('INSERT INTO orders SELECT * FROM orders_tmp'); 
$this->db->query('UNLOCK TABLES'); 
+2

你能解释一下为什么这个工程? – AJFarmar 2015-04-10 20:44:36

1

你要获得一个锁在查询所有表,不只是你写表。所以你的情况,你还需要在orders_tmp读锁定。

从文档:

A session that requires locks must acquire all the locks that it needs in a single LOCK TABLES statement. While the locks thus obtained are held, the session can access only the locked tables.

文档浏览:https://dev.mysql.com/doc/refman/5.5/en/lock-tables.html

干杯

+0

这答案是低质量的帖子,因为它太小了。请说明/原因展开它。否则, ,其他人可能投票将其删除。 – 2015-12-29 00:40:24

+0

@RohitGupta完成 – Madbreaks 2015-12-30 00:56:13