The Easiest Way to Save and Share Code Snippets on the web

Dumping data from tables with similar prefix

python | by: thierrylam

last edit: Sep, 4th 2009 | jump to bottom

import os
 
import MySQLdb
 
user = 'root'
passwd = 'TOPSECRET'
database = 'mydb'
 
db = MySQLdb.connect(user=user, passwd=passwd, db=database)
c = db.cursor()
c.execute("show tables where tables_in_" + database + " like 'hello%'")
result = c.fetchall()
 
tables = ' '.join(row[0] for row in result)
 
sql = 'mysqldump -c -u %s -p%s %s --tables %s > custom.sql' % (user, passwd, database, tables)
os.system(sql)
c.close()
38 views