Python 如何实现数据库表结构同步


Posted in Python onSeptember 29, 2020

近日,某个QQ 群里的一个朋友提出一个问题,如何将一个DB 的表结构同步给另一个DB。
针对这个问题,我进行了思考与实践,具体的实现代码如下所示:

# 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("---------------------打印查询结果----------------------")
    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 如何实现数据库表结构同步的详细内容,更多关于Python 数据库表结构同步的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python进阶教程之函数参数的多种传递方法
Aug 30 Python
Python合并字符串的3种方法
May 21 Python
Python使用BeautifulSoup库解析HTML基本使用教程
Mar 31 Python
python代码 if not x: 和 if x is not None: 和 if not x is None:使用介绍
Sep 21 Python
Python实现迭代时使用索引的方法示例
Jun 05 Python
Django基础知识与基本应用入门教程
Jul 20 Python
Python Requests库基本用法示例
Aug 20 Python
深入了解和应用Python 装饰器 @decorator
Apr 02 Python
Python面向对象总结及类与正则表达式详解
Apr 18 Python
使用Python实现企业微信的自动打卡功能
Apr 30 Python
详解如何管理多个Python版本和虚拟环境
May 10 Python
python thrift 实现 单端口多服务的过程
Jun 08 Python
scrapy-redis分布式爬虫的搭建过程(理论篇)
Sep 29 #Python
python ssh 执行shell命令的示例
Sep 29 #Python
Scrapy基于scrapy_redis实现分布式爬虫部署的示例
Sep 29 #Python
浅析python 字典嵌套
Sep 29 #Python
详解基于Scrapy的IP代理池搭建
Sep 29 #Python
Python 创建守护进程的示例
Sep 29 #Python
Python 解析xml文件的示例
Sep 29 #Python
You might like
动态表单验证的操作方法和TP框架里面的ajax表单验证
2017/07/19 PHP
js文字滚动停顿效果代码
2008/06/28 Javascript
javascript IE中的DOM ready应用技巧
2008/07/23 Javascript
Ext 今日学习总结
2010/09/19 Javascript
jQuery性能优化28条建议你值得借鉴
2013/02/16 Javascript
jquery进行数组遍历如何跳出当前的each循环
2014/06/05 Javascript
JQuery中serialize() 序列化
2015/03/13 Javascript
JS实现点击复选框将按钮或文本框变为灰色不可用的方法
2015/08/11 Javascript
js+html5操作sqlite数据库的方法
2016/02/02 Javascript
树结构之JavaScript
2017/01/24 Javascript
js实现自定义路由
2017/02/04 Javascript
Bootstrap3多级下拉菜单
2017/02/24 Javascript
使用jquery模拟a标签的click事件无法实现跳转的解决
2018/12/04 jQuery
vue的路由映射问题及解决方案
2019/10/14 Javascript
使用webpack5从0到1搭建一个react项目的实现步骤
2020/12/16 Javascript
[06:57]DOTA2-DPC中国联赛 正赛 Ehome vs PSG.LGD 选手采访
2021/03/11 DOTA
python实现的解析crontab配置文件代码
2014/06/30 Python
为python设置socket代理的方法
2015/01/14 Python
python使用Flask框架获取用户IP地址的方法
2015/03/21 Python
Python实现处理管道的方法
2015/06/04 Python
Python根据区号生成手机号码的方法
2015/07/08 Python
解决pandas 作图无法显示中文的问题
2018/05/24 Python
Python闭包函数定义与用法分析
2018/07/20 Python
python 3.6.2 安装配置方法图文教程
2018/09/18 Python
Django REST framework视图的用法
2019/01/16 Python
Python 用turtle实现用正方形画圆的例子
2019/11/21 Python
selenium学习教程之定位以及切换frame(iframe)
2021/01/04 Python
Java中实现多态的机制是什么?
2014/12/07 面试题
成人教育自我鉴定
2013/11/01 职场文书
机械设计毕业生自荐信
2014/02/02 职场文书
大学生入党推荐书范文
2014/05/17 职场文书
党的群众路线教育实践活动教师自我剖析材料
2014/10/09 职场文书
继承权公证书范本
2015/01/23 职场文书
教师工作证明范本
2015/06/12 职场文书
工作感言一句话
2015/08/01 职场文书
MySQL中dd::columns表结构转table过程及应用详解
2022/09/23 MySQL