MySQL and lock a table, read, and then truncate

2024/11/13 12:57:57

I am using mysqldb in python.

I need to do the following for a table.

1) Lock
2) Read
3) Truncate the table
4) Unlock

When I run the below code, I get the below error. So, I am rather unsure on how to lock a table for reading it, then truncating the table. I need to be sure that no other connection reads the data.

asin_list = [] conn = MySQLdb.connect(host=parms['database']['operations']['host'],user=parms['database']['operations']['username'],passwd=parms['database']['operations']['password'],db=parms['database']['operations']['database'])cursor = conn.cursor()query = "LOCK TABLES asin_one_time_only READ"cursor.execute(query)print 'fu1'query = """select asin FROM asin_one_time_only""" cursor.execute(query)rows = cursor.fetchall()for row in rows:asin_list.append(row[0]) print asin_listprint 'fu2'query = "UNLOCK TABLES;"cursor.execute(query)conn.commit()print 'fu3'query = "LOCK TABLES asin_one_time_only WRITE"cursor.execute(query)query = """truncate table amz_one_time_only""" cursor.execute(query)conn.commit()print 'fu3'query = "UNLOCK TABLES;"cursor.execute(query)conn.commit()cursor.close()conn.close() Traceback (most recent call last):File "/home/ubuntu/workspace/Amazon-Products-Crawler-1/threaded_crawl.py", line 1086, in <module>onetime = getOneTimeOnlyAsins(parms)File "/home/ubuntu/workspace/Amazon-Products-Crawler-1/threaded_crawl.py", line 109, in getOneTimeOnlyAsinscursor.execute(query)File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 166, in executeself.errorhandler(self, exc, value)File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 35, in defaulterrorhandlerraise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1192, "Can't execute the given command because you have active locked tables or an active transaction")
Answer

You can't truncate a table that is locked for writing. This is because "truncate" means "destroy the table, and recreate a new one with the same schema."

You can however, empty the table. Instead of TRUNCATE TABLE asin_one_time_only use DELETE FROM asin_one_time_only. Note that this will not reset the autoincrement numbering. If you want to reset it as well, use ALTER TABLE asin_one_time_only auto_increment=1

I suggest doing this:

LOCK TABLES asin_one_time_only READ;
SELECT asin FROM asin_one_time_only;
-- minimize the possibility of someone writing to the table in-between
-- an "UNLOCK TABLES" and a "LOCK TABLES" by just issuing a new LOCK TABLES
-- I am not 100% sure that MySQL will do this atomically, so there is a
-- possibility that you may delete a row that was not read.
-- If this is unacceptable, then use a "LOCK TABLES asin_one_time_only WRITE"
-- from the very beginning.
LOCK TABLES asin_one_time_only WRITE;
DELETE FROM asin_one_time_only;
ALTER TABLE asin_one_time_only auto_increment=1;
UNLOCK TABLES;
https://en.xdnf.cn/q/72102.html

Related Q&A

Train and predict on variable length sequences

Sensors (of the same type) scattered on my site are manually reporting on irregular intervals to my backend. Between reports the sensors aggregate events and report them as a batch. The following datas…

What should a Python project structure look like for Travis CI to find and run tests?

I currently have a project with the following .travis.yml file:language: python install: "pip install tox" script: "tox"Locally, tox properly executes and runs 35 tests, but on Trav…

Having trouble building a Dns Packet in Python

Im trying to build a dns packet to send over a socket. I dont want to use any libraries because I want direct access to the socket variable that sends it. Whenever I send the DNS packet, wireshark says…

Element wise comparison between 1D and 2D array

Want to perform an element wise comparison between an 1D and 2D array. Each element of the 1D array need to be compared (e.g. greater) against the corresponding row of 2D and a mask will be created. He…

Jquery ajax post request not working

I have a simple form submission with ajax, but it keeps giving me an error. All the error says is "error". No code, no description. No nothing, when I alert it when it fails.Javascript with …

Why is the main() function not defined inside the if __main__?

You can often see this (variation a):def main():do_something()do_sth_else()if __name__ == __main__:main()And I am now wondering why not this (variation b):if __name__ == __main__:do_something()do_sth_e…

Using selenium inside gitlab CI/CD

Ive desperetaly tried to set a pytest pipeline CI/CD for my personal projet hosted by gitlab. I tried to set up a simple project with two basic files: file test_core.py, witout any other dependencies f…

VSCode issue with Python versions and environments from Jupyter Notebooks

Issue: I am having issues with the environment and version of Python not matching the settings in VSCode, and causing issues with the packages I am trying to use in Jupyter notebooks. I am using a Wind…

weighted covariance matrix in numpy

I want to compute the covariance C of n measurements of p quantities, where each individual quantity measurement is given its own weight. That is, my weight array W has the same shape as my quantity ar…

How to implement biased random function?

My question is about random.choice function. As we know, when we run random.choice([apple,banana]), it will return either apple or banana with equal probabilities, what if I want to return biased resul…