Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Python to synchronize the table structure of one DB to another DB

Shulou Source: shulou.com Published: 2022-06-01 19:41:11 09月26日 Update

This article mainly introduces "how to use Python to synchronize the table structure of one DB to another DB". In daily operation, I believe that many people have doubts on how to use Python to synchronize the table structure of one DB to another DB. Xiaobian consulted all kinds of data and sorted out a simple and useful method of operation. I hope it will be helpful to answer the doubt of "how to use Python to synchronize the table structure of one DB to another DB". Next, please follow the editor to study!

The specific implementation code is as follows:

# coding:utf-8import 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 (Columbia [CloNum] [0])] = val [cloNum] resultList.append (tempDict) print ("- print query results--") 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) so far The study on "how to use Python to synchronize the table structure of one DB to another DB" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Structure synchronization learning more help practical next code articles methods theory knowledge articles results websites materials follow questions ease of use practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Xiaomi Microsoft OPPO Reno NVidia