Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock tables with codeigniter?

I have to run this sql routine in a model:

$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');

but I get this error:

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

I use MyISAM as DB engine on this table.

Could you please help me?

like image 596
user1534715 Avatar asked Oct 15 '25 04:10

user1534715


2 Answers

try this

$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');
like image 135
user3934078 Avatar answered Oct 16 '25 20:10

user3934078


To perform many INSERT and SELECT operations on a table real_table when concurrent inserts are not possible, you can insert rows into a temporary table temp_table and update the real table with the rows from the temporary table periodically. This can be done with the following code:

mysql> LOCK TABLES real_table WRITE, temp_table WRITE;

Kindly ask if it not worked for you.

like image 27
Arun Jain Avatar answered Oct 16 '25 21:10

Arun Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!