# -*- coding: utf-8 -*- from mysql.mmysql import MysqlBase import copy import pdb old_db = {'user': 'ad', 'pswd': "vF4j56AfxU3P", 'host': '10.10.3.5', 'db': 'ad'} new_db = {'user': 'ad', 'pswd': "vF4j56AfxU3P", 'host': '10.10.3.5', 'db': 'test1'} old = MysqlBase(**old_db) new = MysqlBase(**new_db) def get_old_ad(): old_data = [] sql = "SELECT id,name,gameid,channelid,locationid,begin_time,end_time,ad_num,ad_title,ad_body,ad_image,jump_param," \ "ad_property,ad_sort,jump_status,status,in_used,companyid,createtime FROM `ad` ; " data = old.query(sql) if data: for line in data: if line: temp = {} temp['id'], temp['name'], temp['gameid'], temp['channelid'], temp['locationid'], temp['begin_time'], \ temp['end_time'], temp['ad_num'], temp['ad_title'], temp['ad_body'], temp['ad_image'], temp[ 'jump_param'], temp['ad_property'], temp['ad_sort'], temp['jump_status'], temp['status'], temp[ 'in_used'], temp['companyid'], temp['createtime'] = line old_data.append(temp) del temp return old_data def write_ad_data(data): for line in data: table_name = "ad" try: new.insert(table_name, line) except Exception: print(f"write {line} 2 ad failed!") def get_old_location(): location_data = [] sql = "SELECT id,area,gameid,channelid,type,mode,in_used,createtime FROM `location` " data = old.query(sql) if data: for line in data: if line: temp = {} temp['id'], temp['area'], temp['gameid'], temp['channelid'], temp['type'], temp['mode'], temp[ 'in_used'], temp['createtime'] = line temp['ld_property'] = "" try: area_old = copy.deepcopy(temp['area']) if len(area_old.split(',')) != 5: print(f"area can`t split {area_old}") else: temp['x'] = area_old.split(',')[1] temp['y'] = area_old.split(',')[2] temp['x_offset'] = area_old.split(',')[3] temp['y_offset'] = area_old.split(',')[4] temp['area'] = f"[{area_old.split(',')[0]}]" location_data.append(temp) del temp except Exception: print(f"split area failed,area={temp['area']} ") return location_data def write_location(data): for line in data: table_name = "location" try: new.insert(table_name, line) except Exception: print(f"write {line} 2 location failed!") def main(): old_ad = get_old_ad() write_ad_data(old_ad) old_location = get_old_location() write_location(old_location) if __name__ == "__main__": main()