Python 如何實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)同步
近日,某個QQ 群里的一個朋友提出一個問題,如何將一個DB 的表結(jié)構(gòu)同步給另一個DB。
針對這個問題,我進行了思考與實踐,具體的實現(xiàn)代碼如下所示:
# coding:utf-8
import pymysql
dbDict = {"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)
示例代碼操作簡單,通俗易懂,所以沒有過多的注釋,如有疑問的小伙伴們,可在文章下方評論。
以上就是Python 如何實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)同步的詳細內(nèi)容,更多關于Python 數(shù)據(jù)庫表結(jié)構(gòu)同步的資料請關注腳本之家其它相關文章!
- 使用Python實現(xiàn)將多表分批次從數(shù)據(jù)庫導出到Excel
- python如何解析復雜sql,實現(xiàn)數(shù)據(jù)庫和表的提取的實例剖析
- python的mysql數(shù)據(jù)庫建立表與插入數(shù)據(jù)操作示例
- python 獲取sqlite3數(shù)據(jù)庫的表名和表字段名的實例
- Python獲取數(shù)據(jù)庫數(shù)據(jù)并保存在excel表格中的方法
- Python實現(xiàn)將MySQL數(shù)據(jù)庫表中的數(shù)據(jù)導出生成csv格式文件的方法
- Python實現(xiàn)mysql數(shù)據(jù)庫更新表數(shù)據(jù)接口的功能
- Python實現(xiàn)將sqlite數(shù)據(jù)庫導出轉(zhuǎn)成Excel(xls)表的方法
- Python MySQL進行數(shù)據(jù)庫表變更和查詢
- Python如何讀取MySQL數(shù)據(jù)庫表數(shù)據(jù)
- python數(shù)據(jù)庫操作常用功能使用詳解(創(chuàng)建表/插入數(shù)據(jù)/獲取數(shù)據(jù))
相關文章
Python之ThreadPoolExecutor線程池問題
這篇文章主要介紹了Python之ThreadPoolExecutor線程池問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
Python callable內(nèi)置函數(shù)原理解析
這篇文章主要介紹了Python callable內(nèi)置函數(shù)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
Phantomjs抓取渲染JS后的網(wǎng)頁(Python代碼)
phantomjs:我的理解就是它是一個無顯示的瀏覽器,也就是說除了不能顯示頁面內(nèi)容以外,瀏覽器能干的活兒它基本上都能干。下面我們就來利用他做點有趣的事情2016-05-05
使用matplotlib創(chuàng)建Gif動圖的實現(xiàn)
本文主要介紹了使用matplotlib創(chuàng)建Gif動圖的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04

