Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report