場(chǎng)景:由于開發(fā)惡心的賬號(hào)登錄設(shè)置,鑒于日本玩家用戶習(xí)慣,剛開始以游客的身份登錄游戲,若發(fā)生換包或者換設(shè)備,都需要從數(shù)據(jù)庫(kù)重新更換賬號(hào)ID,剛開始純手工,其后py實(shí)現(xiàn),封裝為exe直接在dos里面執(zhí)行,最好的方式改進(jìn)當(dāng)然是在web見面一鍵完成,現(xiàn)將代碼跑路,作為記錄。

創(chuàng)新互聯(lián)是一家專注于網(wǎng)站制作、
成都做網(wǎng)站與策劃設(shè)計(jì),站前網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:站前等地區(qū)。站前做網(wǎng)站價(jià)格咨詢:13518219792# coding=utf-8
import pymysql
import traceback
import simplejson
import sys
# 交換
def exchange(connect_index, file_name):
try:
# 讀取數(shù)據(jù)庫(kù)配置
fp = open("config.json")
connect_info = simplejson.load(fp)["db_list"]
fp.close()
# 創(chuàng)建鏈接
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')
# 獲取光標(biāo)
cur = conn.cursor()
# 讀取交換列表
fp = open(str(file_name))
exchange_data = simplejson.load(fp)
fp.close()
# 開始交換
for item in exchange_data:
# 讀取兩個(gè)值的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以免發(fā)生沖突
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文件 還未整理,若有疑問請(qǐng)問博主,反正他也不會(huì)告訴你。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享標(biāo)題:記錄一段python代碼(修改數(shù)據(jù)庫(kù)字符串)-創(chuàng)新互聯(lián)
文章來源:
http://weahome.cn/article/dhpcso.html