python - MySQL 并锁定一个表,读取,然后截断

标签 python mysql mysql-python

我在 python 中使用 mysqldb。

我需要对表格执行以下操作。

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

当我运行以下代码时,出现以下错误。所以,我不太确定如何锁定一个表来读取它,然后截断该表。我需要确保没有其他连接读取数据。

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")

最佳答案

您不能截断为写入而锁定的表。这是因为“截断”意味着“销毁表,并使用相同的模式重新创建一个新表。”

不过,您可以清空表格。而不是 TRUNCATE TABLE asin_one_time_only 使用 DELETE FROM asin_one_time_only。请注意,这不会重置自动增量编号。如果您也想重置它,请使用 ALTER TABLE asin_one_time_only auto_increment=1

我建议这样做:

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;

关于python - MySQL 并锁定一个表,读取,然后截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11921366/

相关文章:

python - 无效过滤器 : Only one property per query may have inequality filters (>=, <=、>、<)

jquery - 如何使用 amchart 添加多条曲线取决于您的独特数据

MySQL 插入与更新

php - MySQL 错误 : You have an error in your SQL syntax (ENGINE=MyISAM)

python - Python 中有写入 MySQL 数据库的权限吗?

python - 为什么我不能跳出这个 itertools 无限循环?

python - 如何重新排序Python十六进制字符串中的字节并转换为长整型

python - Python 中的位反转

python - 在 MySql 中存储一个 Pickle

python - 使用 MySQLdb 执行 INSERT 查询但表为空