Python数据库同步神器(一键同步)

article/2025/8/26 14:41:23

1.背景:

最近公司项目的用户中心模块经常出现线上问题,测试人员需要将线上真实数据导入到测试环境中去复现Bug。公司有3套测试环境,来回切换并校验数据比较麻烦,于是就有了如下的数据库同步神器。

2.界面:

3.源代码:

# -*- coding: utf-8 -*-
"""
@date:  2022/01/16 22:00
@author: Anker
@python:v3.6
"""
import json
import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox
import pymysql as pymysql
from tkinter import END
from pymysql.cursors import DictCursor# 线上数据库
online_connect = "erdshggadkjhg2wrtyuic.sfgh.aliyuncs.com"
online_user = "Anker"
online_password = "123456"
online_port = "3306"
online_db_name = "user_center"
online_table = "uuc_business_user"# 测试数据库
test_connect = "bm-cp134dfhdh5e7uho.mysql.rds.aliyuncs.com"
test_user = "root"
test_port = "3306"
test_password = "Vr9ybte9hQJMghjkljh"
test_table = "uuc_business_user"# 创建一个窗口
window = tk.Tk()
window.title("数据库同步工具")
window.geometry('600x600')# 将new_user_data变量初始化
new_user_data = Nonedef data_synchronization():# 定义一个全局变量new_user_dataglobal new_user_data# 每次触发操作之前,清空多行文本框的内容text.delete("1.0", "end")# 定义两个变量name和phone,用来获取用户在文本框输入的内容name = entry1.get()phone = entry2.get()# 连接线上数据库数据库conn_online_sql = pymysql.connect(host=online_connect, user=online_user, cursorclass=DictCursor,password=online_password, database=online_db_name)curs_online = conn_online_sql.cursor()# 通过字典去读取不同的测试环境db_config = radio.get()config = {"0": "0", "test1环境": "user_center_ding", "test2环境": "user_center_ding_test2", "test3环境": "test3"}test_db_name = config[db_config]try:if not (entry1.get() and entry2.get()):tk.messagebox.showinfo(title="提示", message="用户姓名、用户手机为必填项!")elif radio.get() == "0":tk.messagebox.showinfo(title="提示", message="请选择数据库")elif radio.get() == "test3环境":tk.messagebox.showinfo(title="提示", message="目前没有该测试数据库")else:# 连接测试数据库数据库"conn_test2_sql = pymysql.connect(host=test_connect, user=test_user, password=test_password,database=test_db_name)curs2 = conn_test2_sql.cursor()# 读取线上数据库的用户信息read_online_sql = f'''select * from `{online_db_name}`.`{online_table}` where name = "{name}" and phone = "{phone}" '''# 读取测试数据库的用户信息read_test2_sql = f'''select * from `{test_db_name}`.`{test_table}` where name = "{name}" and phone = "{phone}" '''# 执行sql查询,查询线上数据库,将查询出来的结果保存在online_user_data中curs_online.execute(read_online_sql)online_user_data = curs_online.fetchone()print("线上数据库该用户数据为:" + '\n' + str(online_user_data) + '\n')# 判断线上数据库是否有该用户if not online_user_data:text.insert("insert", "线上数据库没有该用户!" + '\n')else:# 执行下面这句后data为{"content": "x", "created_time": "2019-06-19 13:28:30"},是str类型data = json.dumps(online_user_data, default=str, ensure_ascii=False)# 把data转换为字典类型data_result = json.loads(data)print("转换为dict类型后的数据为:", '\n', data_result, '\n')# 把data再次转为json类型json_result = json.dumps(data_result)print("转换为json类型后的数据为:" + '\n' + json_result + '\n')# 获取返回字典data_result值的所有value的列表new_user_data = tuple(data_result.values())print(new_user_data)# 执行sql查询,查询测试数据库,将查询出来的结果保存在test_user_data中curs2.execute(read_test2_sql)test2_user_data = curs2.fetchone()# 提交数据至线上数据库conn_online_sql.commit()# 判断测试数据库是否有该用户if test2_user_data:text.insert("insert", "测试数据库已存在该用户数据!" + '\n')# 如果线上数据库有该用户,测试数据库没有该用户,则执行同步操作if online_user_data and (not test2_user_data):try:# 插入的SQL语句insert_sql = f'''insert into `{test_db_name}`.`{test_table}` VALUES {new_user_data}'''# 执行SQL,向测试数据库中插入数据curs2.execute(insert_sql)print(insert_sql)text.insert("insert", "数据同步成功!" + '\n')except IOError:text.insert("insert", "数据同步失败!" + '\n')conn_test2_sql.commit()curs_online.close()curs2.close()except IOError:print("数据插入失败")conn_online_sql.rollback()tkinter.filedialog.Directory()conn_online_sql.close()# 清空按钮函数:用来清空输入文本框中的内容
def clear():entry1.delete(0, END)  # 清空Entry文本框中的内容entry2.delete(0, END)  # 清空Entry文本框中的内容text.delete("1.0", "end")  # 清空text多行文本框中的内容radio.set("0")  # 清空Radiobutton标签选项框def selection():text.delete("1.0", "end")choose = str(radio.get())text.insert("insert", "您选择的是:" + choose + '\n')# 创建一个标签
tk.Label(window, text="线上用户姓名:").place(x=10, y=50)
tk.Label(window, text="线上用户手机:").place(x=10, y=120)
# 创建一个Entry文本框:用来输入一行文本字符串
entry1 = tk.Entry(window, bd=3, width=62, highlightcolor='red')
entry1.place(x=120, y=50)
entry2 = tk.Entry(window, bd=3, width=62, highlightcolor='green')
entry2.place(x=120, y=120)
# 创建一个可以输入多行文字的文本框
text = tk.Text(window, height=20, width=75, bg='white', highlightcolor='blue', highlightthickness=1)
text.place(x=35, y=240)
# 创建两个按钮:选择文件夹、退出窗口
tk.Button(window, text='开始同步', bg='grey', fg='white', command=data_synchronization).place(x=130, y=520)
tk.Button(window, text='清空', bg='grey', fg='white', command=clear).place(x=290, y=520)
tk.Button(window, text='退出', bg='grey', fg='white', command=window.quit).place(x=430, y=520)radio = tk.StringVar()
radio.set("0")
tk.Label(window, text='请选择数据库:', bg='green', fg='white', width=12).place(x=10, y=180)
# 创建3个radiobutton选项
radio1 = tk.Radiobutton(window, text='test1环境', variable=radio, command=selection, value="test1环境")
radio1.place(x=140, y=180)
radio2 = tk.Radiobutton(window, text='test2环境', variable=radio, command=selection, value="test2环境")
radio2.place(x=290, y=180)
radio3 = tk.Radiobutton(window, text='test3环境', variable=radio, command=selection, value="test3环境")
radio3.place(x=440, y=180)# 主循环
window.mainloop()


http://chatgpt.dhexx.cn/article/etRl7ppD.shtml

相关文章

分布式数据库同步

分布式部署时数据库之间的数据同步 数据的同步原理就是同步binlog日志到需要复制的其他的数据库上,其他数据库根据binlog日志里面的ddl和dml语句,执行该语句同步到当前数据库,就能保证多个数据库的数据的一致性。 Binlog日志主要是的数据库执行的ddl、…

异构数据库同步方案

目录 1 概述 2 原理 3 参数 1 概述 为减轻生产库负载,避免在其上直接运行分析应用拖垮系统,需要将生产系统产生的业务数据实时同步到大数据分析平台。 凭借异构(主从库不同类型、主从对象不同属主模式)数据处理能力&#xff…

IDEA 之because it is included into a circular dependency循环依赖的解决办法

问题场景: 今天启动项目的时候突然遇到这个错误导致无法启动 Information:2019/8/26 11:34 - Compilation completed with 1 error and 0 warnings in 6 s 52 ms Error:Cannot build artifact aws_multi_branch_1.0.0:war exploded because it is included into a…

Maven dependencyManagement 详解

dependencyManagement(以下简称:《依赖管理器》) 《依赖管理器》简介 Maven中的《依赖管理器》元素提供了一种管理依赖版本号的方式。在《依赖管理器》元素中声明所依赖的jar包的版本号等信息,那么所有子项目再次引入此依赖jar包…

Maven之dependencyManagement

1. 父项目的dependencyManagement 最开始,知道dependencyManagement是管理jar包版本的,如果在父项目中的该节点下声明了包的版本,子项目中在Dependencies中引用该包时就不需要声明版本了,这样保证多个子项目能够使用相同的包版本…

dependency-track 初始化源码解析

因项目的关键因素,需查看dependency-check源码流程。个人学习用。自己读代码可以根据web.xml的几个listener来debug。 1、org.dependencytrack.RequirementsVerifier 校验java版本,内存等运行时环境。 2、org.dependencytrack.upgrade.UpgradeInitiali…

Maven中的dependencyManagement 详解

Maven中的dependencyManagement 详解 大家好,我是酷酷的韩金群~ 1.作用: 在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器。 2.pom.xml文件中,jar的版本判断的两种途径: (1)如果dependencies里的dependency自己…

dependency walker工具简介及使用

dependency walker工具 简介使用 简介 官方概述: Dependency Walker is a free utility that scans any 32-bit or 64-bit Windows module (exe, dll, ocx, sys, etc.) and builds a hierarchical tree diagram of all dependent modules. For each module found, …

pytest.mark.dependency用例依赖

这是一个pytest第三方插件,主要解决用例之间的依赖关系。如果依赖的上下文失败后续的用例会被标识为跳过执行,相当于执行了pytest.mark.skip。 1.安装 安装命令如下: pip install pytest-dependency执行上述命令后,再执行pip i…

dependencyManagement使用简介

dependencyManagement使用简介 Maven中的dependencyManagement元素提供了一种管理依赖版本号的方式。在dependencyManagement元素中声明所依赖的jar包的版本号等信息,那么所有子项目再次引入此依赖jar包时则无需显式的列出版本号。Maven会沿着父子层级向上寻找拥有…

Dependency-check

文章目录 前言工具简介工具原理原理检测过程NVDCVSS 工具安装工具地址环境依赖工具安装Jenkins PluginCommand LineOn *nixOn WindowsOn Mac (rec) Maven PluginAnt Task 使用示例命令行方式(Mac)插件方式 报告解读CSV格式报告HTML格式报告 工具对比参考 前言 公司…

maven中dependency的属性(依赖)配置

groupId,artfactId,version,type,classifier,scope,systemPath,exclusions,optional 是 maven的9种依赖属性, 其中groupId,artfactId,version是三…

Maven -- dependency详解

PS&#xff1a;部分来源官网文档&#xff0c;翻译不到位&#xff0c;请移步官网 一 &#xff1a;type&#xff1a;个人理解&#xff1a;依赖<dependency>通过其子标签 定位了某个特定的唯一构件&#xff0c;所以type--依赖类型&#xff0c;更准确的说应该是依赖的构件…

MySQL中“full outer join“的实现

一: 先创建两个表 二: 使用【left join】 union 【right join】 select t1.dim_a, t1.qty qty_a, t2.dim_a dim_b, t2.qty qty_b from ta t1 left join tb t2 on t1.dim_at2.dim_a union select t1.dim_a, t1.qty qty_a, t2.dim_a dim_b, t2.qty qty_b from ta t1 right join…

left join 和 left outer join 的区别

通俗的讲&#xff1a; A left join B 的连接的记录数与A表的记录数同 A right join B 的连接的记录数与B表的记录数同 A left join B 等价B right join A table A: Field_K, Field_A 1 a 3 b 4 c table B: …

SQL中inner join、outer join和cross join的区别

缺省情况下是inner join,开发中使用的left join和right join属于outer join,另外outer join还包括full join.下面我通过图标让大家认识它们的区别。 现有两张表&#xff0c;Table A 是左边的表。Table B 是右边的表。其各有四条记录&#xff0c;其中有两条记录name是相同的&…

SQL Server中CROSS APPLY和OUTER APPLY应用

1.Cross Apply和Outer Apply的理解 新增的APPLY表运算符把右表表达式应用到左表表达式中的每一行。 它不像JOIN那样先计算哪个表表达式都可以&#xff0c;APPLY必选先逻辑地计算左表达式。这种计算输入的逻辑顺序允许把右表达式关联到左表表达式。 APPLY有两种形式&#xff0…

深夜学习:有关Inner、Outer等相关词汇的理解

快速链接: . 👉👉👉 个人博客笔记导读目录(全部) 👈👈👈 付费专栏-付费课程 【购买须知】:【精选】ARMv8/ARMv9架构入门到精通-[目录] 👈👈👈引流关键词: 内存屏障, DSB,DMB,ISB,inner,outer,memory barrier,Non-cacheable,Cacheable, non-shareable,inner…

外连接(OUTER JOIN)

9.3.4 外连接&#xff08;OUTER JOIN&#xff09; 不管是内连接还是带WHERE子句的多表查询&#xff0c;都组合自多个表&#xff0c;并生成结果表。换句话说&#xff0c;如果任何一个源表中的行在另一个源表中没有匹配&#xff0c;DBMS将不把该行放在最后的结果表中。 而外连…

CROSS APPLY和OUTER APPLY的区别

CROSS APPLY和OUTER APPLY的区别 APPLY语法 在SQL中&#xff0c;有这样的一种查询方式&#xff0c;APPLY语法&#xff1a;微软添加了这个新的运算符用于关联一个带有函数的结果集&#xff0c;并把函数应用于表/视图中的每一个限定行中。这个运算符就是APPLY&#xff0c;APPLY的…