python 数据同步-金沙1005

python 数据同步_数据库之间的数据同步1.背景:最近公司项目的用户中心模块经常出现线上问题,测试人员需要将线上真实数据导入到测试环境中去复现bug。公司有3套测试环境,来回切换并校验数据比较麻烦,于是就有了如下的数据库同步神器出现了。ps:鄙人代码写的比较烂,还望各位大神多指点2.界面:3.源代码:#-*-coding:utf-8-*-“””@date:2022/01/1622:00@author:anker@python:v3.6″””importjsonimport..

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

python 数据同步_数据库之间的数据同步

# -*- 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 = none
def data_synchronization():
    # 定义一个全局变量new_user_data
    global 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()
python 数据同步_数据库之间的数据同步

为防止网络爬虫,请关注公众号回复”口令”

激活idea 激活clion
datagrip dataspell
dotcover dotmemory
dottrace goland
phpstorm pycharm
resharper reshac
rider rubymine
webstorm 全家桶
js555888金沙老品牌的版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由思创斯整理,转载请注明出处:https://ispacesoft.com/118805.html

(0)

相关推荐

  • python包管理之poetry的使用 poetry是一个python虚拟环境和依赖管理的工具,之前用pipenv,最近学习httprunner时,接触了poetry。poetry和pipenv类似,另外还提供了打包和发布的功能。官方文档:python-poetry.org/docs/ python项目部署:poetry管理本地环境,上线 …

  • 推荐两个非常实用的,python装饰器[亲测有效]1.超时函数 这个函数的作用在于可以给任意可能会hang住的函数添加超时功能,这个功能在编写外部api调用 、网络爬虫、数据库查询的时候特别有用 timeout装饰器的代码如下: import sig

  • 在python中,输出格式:%d , m , %-6d, d , %.6f的一些区分[通俗易懂]和c/c 编程语言一样%d普通的整数输出i=1sum=0whilei<=100:sum =ii =1print(“1到100的和为:%d”%sum)#1到100的和为:5050m整数输出,整数的宽度是6位,若不足6位,左边补空格1i=12sum=0…

  • python激活码最新_pycharm2021年激活码刚出python2021激活码【在线注册码/序列号/激活成功教程码】,https://ispacesoft.com/idea。详细ieda激活码不妨到思创斯聊编程教程网一起来了解一下吧!

  • pycharm切换conda环境_微博版本切换在哪里在使用pycharm编写项目的时候,有时我们会需要切换交互模式进行简单的代码测试。pycharm底部自带一个pythonconsole,可以快速进入默认的python解释器。比如下图中我的console当前使用的版本是python3.6.8的cpython。但是有的项目打开可能就不是这么简单常见的解释器版本,而是下图这样超级无敌罗里吧嗦的当然,你的pycharm底部菜单没有pytho…

    2022年11月20日
  • 怎么用python连接数据库_python提取数据库数据实现步骤:一、使用mysql.connector1、导入msql的包2、创建连接对象3、使用cursor方法获取操作游标4、fetchall方法获取数据,for循环进行输出5、最后关闭连接对象importmysql.connector#创建连接对象#参数分别为:ip地址,用户名,密码,库名mydb=mysql.connector.connector(host=”192.168.139.128″,user=”root”,passwd=”root”,

    2022年12月16日
  • python如何读写excel文件python读写excel的方式有很多,不同的模块在读写的方法上稍有区别:用xlrd和xlwt进行excel读写;用openpyxl进行excel读写;用pandas进行excel读写;为了方便演示,我这里新建了一个data.xlsx…

    2022年11月24日
  • 模拟退火 python_模拟退火算法什么情况不收敛1、旅行商问题(travelling salesman problem, tsp) 旅行商问题是经典的组合优化问题,要求找到遍历所有城市且每个城市只访问一次的最短旅行路线,即对给定的正权完全图求其总权

发表回复

您的电子邮箱地址不会被公开。

联系金沙1005

关注“java架构师必看”公众号

回复4,添加站长微信。

附言:ispacesoft.com网而来。

关注微信
网站地图