場景:由於開發噁心的賬號登錄設置,鑑於日本玩家用戶習慣,剛開始以遊客的身份登錄遊戲,若發生換包或者換設備,都需要從數據庫重新更換賬號ID,剛開始純手工,其後py實現,封裝爲exe直接在dos裏面執行,最好的方式改進當然是在web見面一鍵完成,現將代碼跑路,作爲記錄。
# coding=utf-8 import pymysql import traceback import simplejson import sys # 交換 def exchange(connect_index, file_name): try: # 讀取數據庫配置 fp = open("config.json") connect_info = simplejson.load(fp)["db_list"] fp.close() # 創建鏈接 conn = pymysql.connect(host=str(connect_info[connect_index]["ip"]), port=int(connect_info[connect_index]["port"]), user=str(connect_info[connect_index]["user"]), passwd=str(connect_info[connect_index]["passwd"]), db=str(connect_info[connect_index]["db_name"]), charset='utf8') # 獲取光標 cur = conn.cursor() # 讀取交換列表 fp = open(str(file_name)) exchange_data = simplejson.load(fp) fp.close() # 開始交換 for item in exchange_data: # 讀取兩個值的account_id player_datas = [] for i in range(2): temp_data = {} cur.execute("select `id`, `account_id` from `character` where `" + str(item["key"][i]) + "` = '" + str(item["value"][i]) + "'") result = cur.fetchall() temp_data["id"] = result[0][0] temp_data["account_id"] = result[0][1] player_datas.append(temp_data) # 更改account_id以免發生衝突 for player_data in player_datas: cur.execute("update `character` set `account_id` = '" + str( player_data["account_id"]) + "_' where `id` = '" + str(player_data["id"]) + "'") conn.commit() # 交換 temp_data = player_datas[0]["account_id"] player_datas[0]["account_id"] = player_datas[1]["account_id"] player_datas[1]["account_id"] = temp_data for player_data in player_datas: cur.execute("update `character` set `account_id` = '" + str( player_data["account_id"]) + "' where `id` = '" + str(player_data["id"]) + "'") conn.commit() conn.close() except Exception: traceback.print_exc() if __name__ == '__main__': try: if len(sys.argv) < 3: print "arg error" sys.exit(1) exchange(int(sys.argv[1]), sys.argv[2]) except Exception: traceback.print_exc()
部分json文件 還未整理,若有疑問請問博主,反正他也不會告訴你。