Python 如何實(shí)現(xiàn)數(shù)據(jù)庫(kù)表結(jié)構(gòu)同步
近日,某個(gè)QQ 群里的一個(gè)朋友提出一個(gè)問(wèn)題,如何將一個(gè)DB 的表結(jié)構(gòu)同步給另一個(gè)DB。針對(duì)這個(gè)問(wèn)題,我進(jìn)行了思考與實(shí)踐,具體的實(shí)現(xiàn)代碼如下所示:
# coding:utf-8import pymysqldbDict = {'test1':'l-beta.test1'}dbUser = 'test'dbPassword = '123456'class DBUtils(): def __init__(self): self.conn = pymysql.connect(dbDict[’test1’], dbUser, dbPassword) self.cursor = self.conn.cursor() def dbSelect(self, sql): print('------------------------------------') print(sql) resultList = [] self.cursor.execute(sql) result = self.cursor.fetchall() columns = self.cursor.description for val in result: tempDict = {} for cloNum in range(len(columns)):tempDict[str(columns[cloNum][0])] = val[cloNum] resultList.append(tempDict) print('---------------------打印查詢結(jié)果----------------------') print(resultList) self.dbClose() return resultList def dbExcute(self, sql): print(sql) self.cursor.execute(sql) self.dbClose() def dbClose(self): self.conn.commit() self.cursor.close() self.conn.close()if __name__ == '__main__': test = DBUtils() result = test.dbSelect('select table_name from information_schema.tables where table_schema=’testdb1’') for dict1 in result: test = DBUtils() create_table_sql = 'create table testdb.%s as select * from testdb1.%s' % (dict1[’table_name’],dict1[’table_name’]) print(create_table_sql) test.dbExcute(create_table_sql)
示例代碼操作簡(jiǎn)單,通俗易懂,所以沒(méi)有過(guò)多的注釋,如有疑問(wèn)的小伙伴們,可在文章下方評(píng)論。
以上就是Python 如何實(shí)現(xiàn)數(shù)據(jù)庫(kù)表結(jié)構(gòu)同步的詳細(xì)內(nèi)容,更多關(guān)于Python 數(shù)據(jù)庫(kù)表結(jié)構(gòu)同步的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python 實(shí)現(xiàn)勞拉游戲的實(shí)例代碼(四連環(huán)、重力四子棋)2. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法3. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)4. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))5. JavaScript數(shù)據(jù)結(jié)構(gòu)之雙向鏈表6. 利用CSS制作3D動(dòng)畫7. 一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解8. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼9. SpringBoot整合log4j日志與HashMap的底層原理解析10. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程
