MySQL批量刪除數據腳本

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import MySQLdb
import time
db=MySQLdb.connect(host="172.16.32.11",user="a",passwd="root1123",port=3307,charset='utf8')
cursor=db.cursor()
sql="select id from test.wqq where del_flag=0 limit 0,50000" ##根據條件找出需要刪除的ID號
cursor.execute(sql)
data=cursor.fetchall()
counter=0
data_len=len(data)
for i in range(data_len):
    id=data[i][0]
    counter=counter+1
    sql2="delete from test.wqq where id="+str(id)
    cursor.execute(sql2)
    data_len=data_len-1
    if counter==10000:  ##多少事物提交一次
        db.commit()
        counter=0
        print "剩餘多少%s條數據未刪除" % data_len
    if data_len==1 or data_len==0: ##刪除最後不滿足1次事務數據
        db.commit()
        print "已經完成批量刪除。。。。。"
        
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章