Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:49:25+00:00 2026-06-09T14:49:25+00:00

I am using mysqldb in python. I need to do the following for a

  • 0

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_list
    print '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 getOneTimeOnlyAsins
    cursor.execute(query)
  File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/pymodules/python2.7/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1192, "Can't execute the given command because you have active locked tables or an active transaction")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:49:26+00:00Added an answer on June 9, 2026 at 2:49 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code, using python 2.6.6 and MySQLdb 1.2.2 causes Commands out of sync; you
I'm using MySQLdb and run into the following problem: STMT=INSERT INTO test_table VALUES (%s,
I am using python 2.7 and MySQLdb, while doing import MySQLdb its throwing below
I am using python and specifically MySQLdb to fill a database, although a code
I am building a threaded class to run MySQL queries using Python and MySQLdb.
I am using mysqldb (the python MySQL) module.. But everytime I try to run.
I installed python-mysqldb using sudo apt-get install python-mysqldb I have tried to use it
I am creating a table in using MYSQLdb in Python tdesc = CREATE TABLE
I am trying to log into my table called acounts using MySQLdb in Python,
I am using Python MySQLDB, and I want to insert this into DATETIME field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.