Record a Python code (modify database String)

Source: Internet
Author: User

Scenario: Due to the development of disgusting account login settings, in view of the Japanese player user habits, just beginning to login as a visitor to the game, if a change of package or equipment, you need to re-replace the account ID from the database, just beginning pure manual, then PY implementation, encapsulation for EXE directly in DOS inside execution, The best way to improve is, of course, to meet one click on the Web and now run the code as a record.



# coding=utf-8import pymysqlimport tracebackimport simplejsonimport sys#  Swap def  exchange (Connect_index, file_name):    try:         #  Read Database Configuration         fp = open ("Config.json")         connect_info = simplejson.load (FP) ["Db_list"]         fp.close ()         #  Create a link         conn = pymysql.connect (Host=str (connect_info[ connect_index]["IP"]),                                port=int (connect_info[connect_index]["Port"]),                 &Nbsp;              user=str (Connect_info [Connect_index] ["User"]),                                passwd=str (connect_ info[connect_index]["passwd"]),                                db =str (connect_info[connect_index]["db_name"]),                                 charset= ' UTF8 ')         #  get cursor          cur = conn.cursor ()         #   Read ExchangeList         fp = open (str (file_name))          exchange_data = simplejson.load (FP)          fp.close ()         #  Start Exchange          for item in exchange_data:             #  read account_id          of two values    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)             #  Change account_id to avoid conflicts             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 ()             #  exchange              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 ()



Part of the JSON file has not been organized, if there is doubt Bo owner, he will not tell you anyway.

This article is from the "lake and Laughter" blog, please make sure to keep this source http://hashlinux.blog.51cto.com/9647696/1913550

Record a Python code (modify database String)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.