Dec 15, 2008

Mysql 批量删除数据表

#!/usr/bin/env python
#coding: utf-8
import MySQLdb

def droptables(tablename):
    remainTable = []
    db = MySQLdb.connect(host = 'localhost',
                        user = 'test',
                        passwd = 'test',
                        db = 'test')
    cursor = db.cursor()
    while True:
        cursor.execute('show tables like "%s%%"' % tablename)
        tables = cursor.fetchall()
        if len(tables) == 0:
            break
        for x in tables:
            try:
                cursor.execute('drop table %s' %x)
                db.commit()
            except:
                #print x
                continue
    cursor.close()
    db.close()

if __name__ == '__main__':
    tablename = 'droptest_'
    droptables(tablename)

No comments:

Post a Comment

您的评论将使我blog更有动力~