您必须先创建该数据库create_engine
from urlparse import urlparse, urlunparse
def recreate_db(url):
parsed = urlparse(url)
#parse url so you know host
host_url = urlunparse((parsed.scheme, parsed.netloc, '/', '', '', ''))
#create_engine without database name
engine = create_engine(host_url, convert_unicode=True)
dbname = parsed.path.strip('/')
engine.execute('commit')
try:
#drop (and clean) database if it exists with raw query
engine.execute('drop database `%s`;'%dbname)
engine.execute('commit')
except OperationalError:
pass
#create database
engine.execute('create database `%s` default character set utf8 ;'%dbname)
engine.execute('commit')
print 'Done cleanup'