運(yùn)用Python快速的對(duì)MySQL數(shù)據(jù)庫進(jìn)行重命名
對(duì)數(shù)據(jù)庫的表進(jìn)行重命名可以使用以下原生sql:
RENAME TABLE old_table TO new_table;
窘境:但是MySQL并沒有直接支持對(duì)數(shù)據(jù)庫進(jìn)行重命名
那么如何運(yùn)用Python快速的對(duì)現(xiàn)有的數(shù)據(jù)庫進(jìn)行重命名呢?
比如項(xiàng)目初期,對(duì)數(shù)據(jù)庫的命名(db_ridingroad)沒有規(guī)劃好,然后在下面創(chuàng)建了大量的表和寫入了大量的數(shù)據(jù),現(xiàn)在需要對(duì)數(shù)據(jù)庫的名字進(jìn)行重命名為(db_news_website)
常規(guī)思路下面的方法步驟較為繁瑣
-- 數(shù)據(jù)庫備份mysqldump ?u [UserName] ?p[Password] ?R [DB_Name] > [DB_Name].sql-- 創(chuàng)建新數(shù)據(jù)庫create database [New_DB_Name];-- 把備份的數(shù)據(jù)導(dǎo)入到新數(shù)據(jù)庫mysql ?u [UserName] ?p[Password] [New_DB_Name] < [DB_Name].sql-- 刪除舊數(shù)據(jù)庫drop database [DB_Name];更快捷的方法
只需要執(zhí)行下面這條命令即可
python rename_database.py old_db_name new_db_name
我們可以使用表重命名的方法,把表重命名到新的數(shù)據(jù)庫之下?;具壿嬋缦拢?/p> 創(chuàng)建新數(shù)據(jù)庫 獲取舊數(shù)據(jù)庫下所有的表名 把表重命名到新的數(shù)據(jù)庫名下 刪除舊數(shù)據(jù)庫
下面使用Python代碼去實(shí)現(xiàn),主要代碼(完整代碼見文末):
def rename_db(old_name, new_name): ''' 數(shù)據(jù)庫重命名 :param old_name: 原來的數(shù)據(jù)庫名 :param new_name: 新數(shù)據(jù)庫名 :return: 成功返回True, 失敗返回False ''' # 獲取所有的表名 sql = '''SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE table_schema=%s''' conn, cursor = context() try:# 創(chuàng)建新數(shù)據(jù)庫名cursor.execute(’create database if not exists {}’.format(new_name))cursor.execute(sql, (old_name, ))results = cursor.fetchall()# 獲取表明,循環(huán)處理放到新的數(shù)據(jù)庫名下for r in results: tb = r[’TABLE_NAME’] rename_sql = '''RENAME TABLE {}.{} to {}.{}'''.format(old_name, tb, new_name, tb) cursor.execute(rename_sql)# 把舊數(shù)據(jù)庫刪掉cursor.execute(’drop database {}’.format(old_name)) except Exception as ex:conn.rollback()print('rename_db Exception: {},{}'.format(sql, ex))return False else:# 如果沒有發(fā)生異常,則提交事務(wù)conn.commit() finally:conn.close() return True使用方法
1.安裝PyMySQL
pip install PyMySQL
2.修改腳本中關(guān)于數(shù)據(jù)庫賬號(hào)配置信息部分
MYSQL_HOST = ’127.0.0.1’MYSQL_PORT = 3306MYSQL_USER = ’ridingroad’MYSQL_PASSWORD = ’xxxxyyyy’MYSQL_DATABASE = ’db_ridingroad’
3.切換到腳本所在目錄,執(zhí)行以下命令即可(數(shù)據(jù)無價(jià),請先mysqldump備份)
python rename_database.py old_db_name new_db_name
完整代碼如下:
import sysimport pymysqlMYSQL_HOST = ’127.0.0.1’MYSQL_PORT = 3306MYSQL_USER = ’ridingroad’MYSQL_PASSWORD = ’xxxxyyyy’MYSQL_DATABASE = ’db_ridingroad’MYSQL_CHARSET = ’utf8’def context(is_dict_cursor=True, database=MYSQL_DATABASE): ''' 創(chuàng)建數(shù)據(jù)庫連接, 數(shù)據(jù)以字典結(jié)構(gòu)返回 :param is_dict_cursor: 是否返回字典結(jié)構(gòu)的數(shù)據(jù) :param database: 默認(rèn)連接的數(shù)據(jù)庫 :return: 返回一個(gè)連接和一個(gè)浮標(biāo) ''' try:config = { ’host’: MYSQL_HOST, ’port’: MYSQL_PORT, ’user’: MYSQL_USER, ’password’: MYSQL_PASSWORD, ’database’: database, ’charset’: MYSQL_CHARSET,}conn = pymysql.connect(**config)if is_dict_cursor: cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)else: cursor = conn.cursor()return conn, cursor except Exception as ex:print('connect database failed, {},{}'.format(400, ex))raise Exception({’code’: 400, ’msg’: ex})def rename_db(old_name, new_name): ''' 數(shù)據(jù)庫重命名 :param old_name: 原來的數(shù)據(jù)庫名 :param new_name: 新數(shù)據(jù)庫名 :return: 成功返回True, 失敗返回False ''' # 獲取所有的表名 sql = '''SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE table_schema=%s''' conn, cursor = context() try:conn.begin()# 創(chuàng)建新數(shù)據(jù)庫名cursor.execute(’create database if not exists {}’.format(new_name))cursor.execute(sql, (old_name, ))results = cursor.fetchall()# 獲取表明,循環(huán)處理放到新的數(shù)據(jù)庫名下for r in results: tb = r[’TABLE_NAME’] rename_sql = '''RENAME TABLE {}.{} to {}.{}'''.format(old_name, tb, new_name, tb) cursor.execute(rename_sql)# 把舊數(shù)據(jù)庫刪掉cursor.execute(’drop database {}’.format(old_name)) except Exception as ex:conn.rollback()print('rename_db Exception: {},{}'.format(sql, ex))return False else:# 如果沒有發(fā)生異常,則提交事務(wù)conn.commit() finally:conn.close() return Trueif __name__ == ’__main__’: old_db = sys.argv[1] new_db = sys.argv[2] rename_db(old_name=old_db, new_name=new_db)
以上就是運(yùn)用Python快速的對(duì)MySQL數(shù)據(jù)庫進(jìn)行重命名的詳細(xì)內(nèi)容,更多關(guān)于python 重命名MySQL數(shù)據(jù)庫的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. vue實(shí)現(xiàn)web在線聊天功能2. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法3. Java Bean與Map之間相互轉(zhuǎn)化的實(shí)現(xiàn)方法4. SpringBoot+TestNG單元測試的實(shí)現(xiàn)5. Springboot 全局日期格式化處理的實(shí)現(xiàn)6. Java使用Tesseract-Ocr識(shí)別數(shù)字7. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼8. Django使用HTTP協(xié)議向服務(wù)器傳參方式小結(jié)9. JAMon(Java Application Monitor)備忘記10. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問題
